kkebo / swift_os

An operating system written in Swift.
MIT License
11 stars 0 forks source link

`print()` does not work on QEMU 9.0 #25

Open kkebo opened 3 weeks ago

kkebo commented 3 weeks ago

Environment

$ qemu-system-aarch64 --version
QEMU emulator version 9.0.0
Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
$ uname -a
Linux Brown-rhinoceros-beetle 6.8.10-400.asahi.fc40.aarch64+16k #1 SMP PREEMPT_DYNAMIC Mon May 27 16:05:41 UTC 2024 aarch64 GNU/Linux
$ swift --version
Swift version 6.0-dev (LLVM bf8bbb910874156, Swift 2a6163f0e50d955)
Target: aarch64-unknown-linux-gnu

Expected behavior

$ qemu-system-aarch64 --version
QEMU emulator version 8.2.2 (qemu-8.2.2-1.fc40)
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
$ make run
swift build --triple aarch64-none-none-elf -c release -Xswiftc -Osize --experimental-lto-mode=full -Xswiftc -experimental-hermetic-seal-at-link
Building for production...
[8/8] Archiving libKernel.a
Build complete! (0.67s)
clang -fuse-ld=lld -target aarch64-none-none-elf -nostdlib -Xlinker -gc-sections -static -T linker.ld .build/release/libKernel.a -o kernel.elf
llvm-objcopy kernel.elf -O binary kernel8.img
/usr/bin/qemu-system-aarch64 -machine raspi3b -kernel kernel8.img -serial stdio -display none
Hello Swift!

Actual behavior

$ make run
swift build --triple aarch64-none-none-elf -c release -Xswiftc -Osize --experimental-lto-mode=full -Xswiftc -experimental-hermetic-seal-at-link
Building for production...
[8/8] Archiving libKernel.a
Build complete! (0.74s)
clang -fuse-ld=lld -target aarch64-none-none-elf -nostdlib -Xlinker -gc-sections -static -T linker.ld .build/release/libKernel.a -o kernel.elf
llvm-objcopy kernel.elf -O binary kernel8.img
qemu-system-aarch64 -machine raspi3b -kernel kernel8.img -serial stdio -display none
kkebo commented 3 weeks ago

If I change --experimental-lto-mode from full to thin, print() works well. So this issue may be related to LTO.

$ make run
swift build --triple aarch64-none-none-elf -c release -Xswiftc -Osize --experimental-lto-mode=thin -Xswiftc -experimental-hermetic-seal-at-link
Building for production...
[8/8] Archiving libKernel.a
Build complete! (0.65s)
clang -fuse-ld=lld -target aarch64-none-none-elf -nostdlib -Xlinker -gc-sections -static -T linker.ld .build/release/libKernel.a -o kernel.elf
llvm-objcopy kernel.elf -O binary kernel8.img
qemu-system-aarch64 -machine raspi3b -kernel kernel8.img -serial stdio -display none
Hello Swift!
kkebo commented 3 weeks ago

Also, if I drop -Xswiftc -Osize from swift build, print() works well.

$ make run
swift build --triple aarch64-none-none-elf -c release --experimental-lto-mode=full -Xswiftc -experimental-hermetic-seal-at-link
Building for production...
[8/8] Archiving libKernel.a
Build complete! (0.67s)
clang -fuse-ld=lld -target aarch64-none-none-elf -nostdlib -Xlinker -gc-sections -static -T linker.ld .build/release/libKernel.a -o kernel.elf
llvm-objcopy kernel.elf -O binary kernel8.img
qemu-system-aarch64 -machine raspi3b -kernel kernel8.img -serial stdio -display none
Hello Swift!
kkebo commented 3 weeks ago

Comparison of readelf --syms kernel.elf

LTO=full:

Symbol table '.symtab' contains 23 entries:                                                                                                                                                
   Num:    Value          Size Type    Bind   Vis      Ndx Name                                                                                                                            
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND                                                                                                                                 
     1: 0000000000080000     0 NOTYPE  LOCAL  DEFAULT    1 $x.0                                                                                                                            
     2: 0000000000080040     0 NOTYPE  LOCAL  DEFAULT    1 $d.1                                                                                                                            
     3: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.6                                                                                                                            
     4: 0000000000080054     0 NOTYPE  LOCAL  DEFAULT    1 $x.0                                                                                                                            
     5: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.5                                                                                                                            
     6: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS ld-temp.o                                                                                                                       
     7: 0000000000080060     0 NOTYPE  LOCAL  DEFAULT    1 $x.0                                                                                                                            
     8: 000000000008006c   388 FUNC    LOCAL  DEFAULT    1 $s6Kernel5kmainyyF                                                                                                              
     9: 0000000000080200     0 NOTYPE  LOCAL  DEFAULT    2 $d.1                                                                                                                            
    10: 000000000008006c     0 NOTYPE  LOCAL  DEFAULT    1 $x.2                                                                                                                            
    11: 0000000000080220    36 OBJECT  LOCAL  DEFAULT    3 mbox                                                                                                                            
    12: 0000000000080210     0 NOTYPE  LOCAL  DEFAULT    2 $d.3                                                                                                                            
    13: 0000000000080220     0 NOTYPE  LOCAL  DEFAULT    3 $d.4                                                                                                                            
    14: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.10                                                                                                                           
    15: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.13                             
    16: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.16                             
    17: 0000000000080060    12 FUNC    GLOBAL PROTECTED    1 kmain                           
    18: 0000000000080054     0 NOTYPE  GLOBAL DEFAULT    1 delay                             
    19: 0000000000080000     0 NOTYPE  GLOBAL DEFAULT    1 _start                            
    20: 0000000000080244     0 NOTYPE  GLOBAL DEFAULT    3 __bss_end                         
    21: 000000000008021d     0 NOTYPE  GLOBAL DEFAULT    2 __bss_start                       
    22: 0000000000000027     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_size

LTO=thin:

Symbol table '.symtab' contains 43 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000080000     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
     2: 0000000000080040     0 NOTYPE  LOCAL  DEFAULT    1 $d.1
     3: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.6
     4: 0000000000080054     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
     5: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.5
     6: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS kernel.swift.bc
     7: 0000000000080060     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
     8: 000000000008006c    96 FUNC    LOCAL  DEFAULT    1 $s6Kernel5kmainyyF
     9: 000000000008006c     0 NOTYPE  LOCAL  DEFAULT    1 $x.1                              
    10: 0000000000080240     0 NOTYPE  LOCAL  DEFAULT    2 $d.4
    11: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.9
    12: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.12
    13: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.15
    14: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS kernel.swift.bc
    15: 0000000000080260     0 NOTYPE  LOCAL  DEFAULT    2 $d.0
    16: 00000000000800cc     0 NOTYPE  LOCAL  DEFAULT    1 $x.1
    17: 00000000000801a0     0 NOTYPE  LOCAL  DEFAULT    1 $x.2
    18: 00000000000801c4     0 NOTYPE  LOCAL  DEFAULT    1 $x.3
    19: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.9
    20: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.12
    21: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.15
    22: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS kernel.swift.bc
    23: 00000000000801e0     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
    24: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.6
    25: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.9
    26: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.12
    27: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS mbox.c
    28: 0000000000080270     0 NOTYPE  LOCAL  DEFAULT    3 $d.0
    29: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.4
    30: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.6
    31: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.8
    32: 0000000000080060    12 FUNC    GLOBAL PROTECTED    1 kmain
    33: 00000000000801e0    92 FUNC    GLOBAL PROTECTED    1 $s6Kernel8mboxCa[...]
    34: 00000000000800cc   212 FUNC    GLOBAL PROTECTED    1 $s6Kernel8initUARTyyF
    35: 00000000000801a0    36 FUNC    GLOBAL PROTECTED    1 $s6Kernel7getcha[...]
    36: 00000000000801c4    28 FUNC    GLOBAL PROTECTED    1 $s6Kernel7putcha[...]
    37: 0000000000080270    36 OBJECT  GLOBAL DEFAULT    3 mbox
    38: 0000000000080054     0 NOTYPE  GLOBAL DEFAULT    1 delay
    39: 0000000000080000     0 NOTYPE  GLOBAL DEFAULT    1 _start
    40: 0000000000080294     0 NOTYPE  GLOBAL DEFAULT    3 __bss_end
    41: 0000000000080270     0 NOTYPE  GLOBAL DEFAULT    2 __bss_start
    42: 0000000000000024     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_size
kkebo commented 3 weeks ago

Without -Xswiftc -Osize:

Symbol table '.symtab' contains 22 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000080000     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
     2: 0000000000080040     0 NOTYPE  LOCAL  DEFAULT    1 $d.1
     3: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.6
     4: 0000000000080054     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
     5: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.5
     6: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS ld-temp.o
     7: 0000000000080060     0 NOTYPE  LOCAL  DEFAULT    1 $x.0
     8: 000000000008006c   624 FUNC    LOCAL  DEFAULT    1 $s6Kernel5kmainyyF
     9: 00000000000802f0     0 NOTYPE  LOCAL  DEFAULT    2 $d.1
    10: 000000000008006c     0 NOTYPE  LOCAL  DEFAULT    1 $x.2
    11: 0000000000080300    36 OBJECT  LOCAL  DEFAULT    3 mbox
    12: 0000000000080300     0 NOTYPE  LOCAL  DEFAULT    3 $d.3
    13: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.9
    14: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.12
    15: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.15
    16: 0000000000080060    12 FUNC    GLOBAL PROTECTED    1 kmain
    17: 0000000000080054     0 NOTYPE  GLOBAL DEFAULT    1 delay
    18: 0000000000080000     0 NOTYPE  GLOBAL DEFAULT    1 _start
    19: 0000000000080324     0 NOTYPE  GLOBAL DEFAULT    3 __bss_end
    20: 0000000000080300     0 NOTYPE  GLOBAL DEFAULT    2 __bss_start
    21: 0000000000000024     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_size
kkebo commented 3 weeks ago

Hello Swift! exists even if LTO is full.

$ objdump -s kernel.elf

kernel.elf:     file format elf64-littleaarch64

Contents of section .text:
 80000 a10038d5 21044092 61000034 5f2003d5  ..8.!.@.a..4_ ..
 80010 ffffff17 61010058 3f000091 61010058  ....a..X?...a..X
 80020 82010018 82000034 3f8400f8 42040051  .......4?...B..Q
 80030 a2ffff35 0b000094 f5ffff17 00000000  ...5............
 80040 00000800 00000000 1d020800 00000000  ................
 80050 27000000 000400f1 e1ffff54 c0035fd6  '..........T.._.
 80060 fd7bbfa9 fd030091 01000094 fd7bbea9  .{...........{..
 80070 f30b00f9 fd030091 08000090 0a000090  ................
 80080 09038252 1f2003d5 cb0c0010 09e4a772  ...R. .........r
 80090 0081c03d 68fd60d3 417dc03d 3f1900b9  ...=h.`.A}.=?...
 800a0 7f2100b9 600500ad a80200b5 08139752  .!..`..........R
 800b0 08e0a772 090140b9 e9ffff37 1f2003d5  ...r..@....7. ..
 800c0 090b0010 2a011d32 0a0900b9 0a0140b9  ....*..2......@.
 800d0 eafff737 0a815eb8 2b011d32 5f010b6b  ...7..^.+..2_..k
 800e0 61ffff54 08000090 0900b052 082542b9  a..T.......R.%B.
 800f0 1f01096b 60000054 00000014 200020d4  ...k`..T.... . .
 80100 13008252 89048052 13e4a772 c0128052  ...R...R...r...R
 80110 68f23fd1 0a0140b9 2a151433 69b23dd1  h.?...@.*..3i.=.
 80120 0a0100b9 3f0100b9 cbffff97 73a23dd1  ....?.......s.=.
 80130 08009852 c0128052 680200b9 c6ffff97  ...R...Rh.......
 80140 08038252 eaff8052 08e4a772 2b008052  ...R...R...r+..R
 80150 0c058052 7f0200b9 09008252 0a2d00b9  ...R.......R.-..
 80160 0a0e8052 0b0d00b9 4bfe8052 0c1100b9  ...R....K..R....
 80170 2c608052 09e4a772 0a1500b9 0b2100b9  ,`.R...r.....!..
 80180 0b098052 0c1900b9 1f2003d5 2a040010  ...R..... ..*...
 80190 0c000090 8c710891 0d0140b9 edff2f37  .....q....@.../7
 801a0 2b0100b9 4b1d4038 5f010ceb 61ffff54  +...K.@8_...a..T
 801b0 090140b9 e9ff2f37 09008252 4a018052  ..@.../7...RJ..R
 801c0 09e4a772 2a0100b9 0a0140b9 eaff2737  ...r*.....@...'7
 801d0 2a0140b9 5ffd0371 a8000054 0b0140b9  *.@._..q...T..@.
 801e0 ebff2f37 4a1d0012 f7ffff17 200020d4  ../7J....... . .
Contents of section .data:
 801f0 08000000 02000000 c0c62d00 00000000  ..........-.....
 80200 24000000 00000000 02800300 0c000000  $...............
 80210 48656c6c 6f205377 69667421 00        Hello Swift!.
kkebo commented 3 weeks ago

QEMU 9.0.1 has been released. I'll give it a try.

kkebo commented 3 weeks ago

Same on QEMU 9.0.1