sorry, i didn't copy test/objdump/gdb stuff from 
https://github.com/ziyao233/tinycc/issues/1, that's pretty long, below are the 
detail:

change:

diff --git a/tccasm.c b/tccasm.c
index 109eb17c..197d07a4 100644
--- a/tccasm.c
+++ b/tccasm.c
@@ -535,12 +535,19 @@ static void asm_parse_directive(TCCState *s1, int global)
             v = asm_int_expr(s1);
         }
     zero_pad:
-        if (sec->sh_type != SHT_NOBITS) {
+        if (sec->sh_type == SHT_PROGBITS && (sec->sh_flags & SHF_EXECINSTR)) {
+            int pad = ind % sec->sh_addralign;
+# if !defined TCC_TARGET_X86_64 && !defined TCC_TARGET_I386
+            // current non-x86 gen_fill_nops requires pad bytes to be multiple 
of 4
+            pad = ((pad + 3) / 4) * 4;
+# endif
+            gen_fill_nops(pad);
+        } else if (sec->sh_type != SHT_NOBITS) {
             sec->data_offset = ind;
             ptr = section_ptr_add(sec, size);
             memset(ptr, v, size);
-        }
-        ind += size;
+            ind += size;
+         }
         break;
     case TOK_ASMDIR_quad:
 #ifdef TCC_TARGET_X86_64

minimal_test.s:

      .global     main
main:
      xorl %eax, %eax
      .p2align 4 # replacing .p2align with .align with specific sizes would 
also lead to broken binary
      ret

./tcc minimal_test.s -o minimal_test.s
./minimal_test

for-loop-test.s:

.data
.balign 1
.Lstring.2:
        .ascii "i = %d\012\000"
/* end data */

.text
.balign 16
.globl main
main:
        endbr64
        pushq %rbp
        movq %rsp, %rbp
        subq $8, %rsp
        pushq %rbx
        movl $0, %ebx
.p2align 4
.Lbb2:
        cmpl $5, %ebx
        jge .Lbb4
        movl %ebx, %esi
        leaq .Lstring.2(%rip), %rdi
        movl $0, %eax
        callq printf
        addl $1, %ebx
        jmp .Lbb2
.Lbb4:
        movl $0, %eax
        popq %rbx
        leave
        ret
.type main, @function
.size main, .-main
/* end function main */

.section .note.GNU-stack,"",@progbits

./tcc for-loop-test.s -o for-loop-test
./for-loop-test

after applying the change, it'll work, otherwise, there will be segmentation 
fault:

objdump before change:

$ objdump -d minimal_test

minimal_tesr:     file format elf64-x86-64


Disassembly of section .text:

00000000006009c0 <main@@Base-0x35>:
  6009c0:       f3 0f 1e fa             endbr64
  6009c4:       31 ed                   xor    %ebp,%ebp
  6009c6:       49 89 d1                mov    %rdx,%r9
  6009c9:       5e                      pop    %rsi
  6009ca:       48 89 e2                mov    %rsp,%rdx
  6009cd:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
  6009d1:       50                      push   %rax
  6009d2:       54                      push   %rsp
  6009d3:       45 31 c0                xor    %r8d,%r8d
  6009d6:       31 c9                   xor    %ecx,%ecx
  6009d8:       48 8b 3d c9 02 40 00    mov    0x4002c9(%rip),%rdi        # 
a00ca8 <main@@Base+0x4002b3>
  6009df:       ff 15 cb 02 40 00       call   *0x4002cb(%rip)        # a00cb0 
<main@@Base+0x4002bb>
  6009e5:       f4                      hlt
  6009e6:       66 2e 0f 1f 84 00 00    cs nopw 0x0(%rax,%rax,1)
  6009ed:       00 00 00
  6009f0:       f3 0f 1e fa             endbr64
  6009f4:       c3                      ret

00000000006009f5 <main@@Base>:
  6009f5:       31 c0                   xor    %eax,%eax
  6009f7:       c3                      ret

Disassembly of section .init:

00000000006009f8 <.init>:
  6009f8:       f3 0f 1e fa             endbr64
  6009fc:       48 83 ec 08             sub    $0x8,%rsp
  600a00:       48 8b 05 b1 02 40 00    mov    0x4002b1(%rip),%rax        # 
a00cb8 <__gmon_start__@Base>
  600a07:       48 85 c0                test   %rax,%rax
  600a0a:       74 02                   je     600a0e <main@@Base+0x19>
  600a0c:       ff d0                   call   *%rax
  600a0e:       48 83 c4 08             add    $0x8,%rsp
  600a12:       c3                      ret

Disassembly of section .fini:

0000000000600a14 <.fini>:
  600a14:       f3 0f 1e fa             endbr64
  600a18:       48 83 ec 08             sub    $0x8,%rsp
  600a1c:       48 83 c4 08             add    $0x8,%rsp
  600a20:       c3                      ret


gdb with core dump for segmentation fault:

$ gdb minimal_test wsl-crash-1777429245-19155-_home_jin_tinycc_tcc-11.dmp
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...
(No debugging symbols found in a.out)

warning: core file may not match specified executable file.
[New LWP 19155]

This GDB supports auto-downloading debuginfo from the following URLs:
  <https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n])
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./tcc -B. -run ssa/asm/p2align-test.s'.
Program terminated with signal SIGSEGV, Segmentation fault.
--Type <RET> for more, q to quit, c to continue without paging--
#0  0x00006540507db002 in ?? ()
(gdb) backtrace
#0  0x00006540507db002 in ?? ()
#1  0x00006540507db2a6 in ?? ()
#2  0x00007b1c1a444fb0 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#3  0x00007ffda1f0f8c0 in ?? ()
#4  0x00007ffda1f0f8b0 in ?? ()
#5  0x0000000000000001 in ?? ()
#6  0x00007ffda1f0f6f0 in ?? ()
#7  0x0000654038414a4e in ?? ()
#8  0x00007ffda1f0f600 in ?? ()
#9  0x00007ffda1f0f8b0 in ?? ()
#10 0x0000000100000000 in ?? ()
#11 0x00006540507d52a0 in ?? ()
#12 0x00000000a1f0f630 in ?? ()
#13 0x00006540507db257 in ?? ()
#14 0x0000654038453c95 in ?? ()
#15 0x00007ffda1f0f8c0 in ?? ()
#16 0x00007ffda1f0f898 in ?? ()
#17 0x63f9c39cb7e728be in ?? ()
#18 0x0000000000000004 in ?? ()
#19 0x0000000000000000 in ?? ()
(gdb) info registers
rax            0x0                 0
rbx            0x7ffda1f0f898      140727320377496
rcx            0x6540507db000      111326902726656
rdx            0x7ffda1f0f8c0      140727320377536
rsi            0x7ffda1f0f8b0      140727320377520
rdi            0x1                 1
rbp            0x7ffda1f0f5d0      0x7ffda1f0f5d0
rsp            0x7ffda1f0f5a8      0x7ffda1f0f5a8
r8             0x6540507db257      111326902727255
r9             0x7                 7
r10            0x7ffda1f0f8c0      140727320377536
r11            0x73e66e7403628b81  8351484003557215105
r12            0x4                 4
r13            0x0                 0
r14            0x654038465118      111326496444696
r15            0x7b1c1a771000      135360633311232
rip            0x6540507db002      0x6540507db002
eflags         0x10246             [ PF ZF IF RF ]
cs             0x33                51
ss             0x2b                43
ds             0x0                 0
es             0x0                 0
fs             0x0                 0
gs             0x0                 0
fs_base        0x7b1c1a640740      135360632063808
gs_base        0x0                 0
(gdb) x/100i $rip
=> 0x6540507db002:      add    %al,(%rax)
   0x6540507db004:      add    %al,(%rax)
   0x6540507db006:      add    %al,(%rax)
   0x6540507db008:      add    %al,(%rax)
   0x6540507db00a:      add    %al,(%rax)
   0x6540507db00c:      add    %al,(%rax)
   0x6540507db00e:      add    %al,(%rax)
   0x6540507db010:      ret
   0x6540507db011:      add    %al,(%rax)
   0x6540507db013:      add    %al,(%rax)
   0x6540507db015:      add    %al,(%rax)
   0x6540507db017:      add    %dl,0x48(%rbp)
   0x6540507db01a:      mov    %esp,%ebp
   0x6540507db01c:      sub    $0x30,%rsp
   0x6540507db023:      mov    %rdi,-0x8(%rbp)
   0x6540507db027:      mov    %rsi,-0x10(%rbp)
   0x6540507db02b:      mov    %rdx,-0x18(%rbp)
   0x6540507db02f:      mov    $0x0,%eax
   0x6540507db034:      mov    %eax,-0x1c(%rbp)
   0x6540507db037:      mov    -0x1c(%rbp),%eax
   0x6540507db03a:      movslq %eax,%rax
   0x6540507db03d:      shl    $0x3,%rax
   0x6540507db041:      mov    0x678(%rip),%rcx        # 0x6540507db6c0
   0x6540507db048:      add    %rax,%rcx
   0x6540507db04b:      mov    0x676(%rip),%rax        # 0x6540507db6c8
   0x6540507db052:      cmp    %rax,%rcx
   0x6540507db055:      je     0x6540507db09e
--Type <RET> for more, q to quit, c to continue without paging--
   0x6540507db05b:      mov    -0x1c(%rbp),%eax
   0x6540507db05e:      mov    %eax,%ecx
   0x6540507db060:      add    $0x1,%eax
   0x6540507db063:      mov    %eax,-0x1c(%rbp)
   0x6540507db066:      movslq %ecx,%rcx
   0x6540507db069:      shl    $0x3,%rcx
   0x6540507db06d:      mov    0x64c(%rip),%rax        # 0x6540507db6c0
   0x6540507db074:      add    %rcx,%rax
   0x6540507db077:      mov    (%rax),%rax
   0x6540507db07a:      mov    %rax,-0x28(%rbp)
   0x6540507db07e:      mov    -0x18(%rbp),%rax
   0x6540507db082:      mov    %rax,%r10
   0x6540507db085:      mov    -0x10(%rbp),%rax
   0x6540507db089:      mov    %rax,%rsi
   0x6540507db08c:      mov    -0x8(%rbp),%eax
   0x6540507db08f:      mov    %rax,%rdi
   0x6540507db092:      mov    %r10,%rdx
   0x6540507db095:      mov    -0x28(%rbp),%r11
   0x6540507db099:      call   *%r11
   0x6540507db09c:      jmp    0x6540507db037
   0x6540507db09e:      leave
   0x6540507db09f:      ret
   0x6540507db0a0:      push   %rbp
   0x6540507db0a1:      mov    %rsp,%rbp
   0x6540507db0a4:      sub    $0x10,%rsp
   0x6540507db0ab:      mov    $0x0,%eax
   0x6540507db0b0:      mov    %eax,-0x4(%rbp)
--Type <RET> for more, q to quit, c to continue without paging--
   0x6540507db0b3:      mov    -0x4(%rbp),%eax
   0x6540507db0b6:      movslq %eax,%rax
   0x6540507db0b9:      shl    $0x3,%rax
   0x6540507db0bd:      mov    0x60c(%rip),%rcx        # 0x6540507db6d0
   0x6540507db0c4:      add    %rax,%rcx
   0x6540507db0c7:      mov    0x60a(%rip),%rax        # 0x6540507db6d8
   0x6540507db0ce:      cmp    %rax,%rcx
   0x6540507db0d1:      je     0x6540507db101
   0x6540507db0d7:      mov    -0x4(%rbp),%eax
   0x6540507db0da:      add    $0xffffffff,%eax
   0x6540507db0dd:      mov    %eax,-0x4(%rbp)
   0x6540507db0e0:      movslq %eax,%rax
   0x6540507db0e3:      shl    $0x3,%rax
   0x6540507db0e7:      mov    0x5e2(%rip),%rcx        # 0x6540507db6d0
   0x6540507db0ee:      add    %rax,%rcx
   0x6540507db0f1:      mov    (%rcx),%rcx
   0x6540507db0f4:      mov    %rcx,-0x10(%rbp)
   0x6540507db0f8:      mov    -0x10(%rbp),%r11
   0x6540507db0fc:      call   *%r11
   0x6540507db0ff:      jmp    0x6540507db0b3
   0x6540507db101:      leave
   0x6540507db102:      ret
   0x6540507db103:      push   %rbp
   0x6540507db104:      mov    %rsp,%rbp
   0x6540507db107:      sub    $0x20,%rsp
   0x6540507db10e:      mov    %rdi,-0x8(%rbp)
   0x6540507db112:      mov    0x568(%rip),%eax        # 0x6540507db680
--Type <RET> for more, q to quit, c to continue without paging--
   0x6540507db118:      mov    %eax,-0xc(%rbp)
   0x6540507db11b:      mov    -0xc(%rbp),%eax
   0x6540507db11e:      cmp    $0x0,%eax
   0x6540507db121:      je     0x6540507db174
   0x6540507db127:      mov    -0xc(%rbp),%eax
   0x6540507db12a:      add    $0xffffffff,%eax
   0x6540507db12d:      mov    %eax,-0xc(%rbp)
   0x6540507db130:      mov    -0xc(%rbp),%eax
   0x6540507db133:      movslq %eax,%rax
   0x6540507db136:      shl    $0x3,%rax
   0x6540507db13a:      lea    0x33f(%rip),%rcx        # 0x6540507db480
   0x6540507db141:      add    %rax,%rcx
   0x6540507db144:      mov    -0xc(%rbp),%eax
   0x6540507db147:      movslq %eax,%rax
   0x6540507db14a:      shl    $0x3,%rax
   0x6540507db14e:      lea    0x42b(%rip),%rdx        # 0x6540507db580
   0x6540507db155:      add    %rax,%rdx
   0x6540507db158:      mov    %rcx,-0x18(%rbp)
   0x6540507db15c:      mov    (%rdx),%rdx
(gdb)


Regards
Jerry
________________________________
发件人: [email protected] 
<[email protected]> 代表 grischka via 
Tinycc-devel <[email protected]>
发送时间: Tuesday, May 5, 2026 4:56:22 PM
收件人: [email protected] <[email protected]>
抄送: grischka <[email protected]>
主题: Re: [Tinycc-devel] [patch]fix asm parse directive zero pad logic

On 04.05.2026 11:08, Jerry Jin wrote:
> Problem:
> ....
>
> Fix:
> ....
>
> Open issue:
> ....

There is missing

Test:
   <how you will want to test your fix after you wrote it>

> Please review attached patch file, thanks!

I'm afraid there is no people here who want to extract patches
from email and run tests for other people ;)

There are some useful switches though for objdump that might
help:

$ objdump --help
$ objdump -d -z --insn-width=1 test.o
$ objdump -j .text -s test.o

Also you might want to write a short test file, for example:

test.S:
    mov $0x12345678, %ebx
    .p2align 4
    ret

$ tcc -c test.S && objdump ... test.o

So this is how you could test your fix.

Of course you also need to know what .p2align is supposed to do
at all, exactly.  If you do not know that then you cannot do
anything because you will not know whether your fix is correct
even when you test it.

>
> Regards
> Jerry

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to