Recent

Author Topic: C code works, same code in FreePascal does not  (Read 4534 times)

TCH

  • Full Member
  • ***
  • Posts: 119
C code works, same code in FreePascal does not
« on: August 22, 2018, 09:05:32 pm »
There is this C code:
Code: C  [Select][+][-]
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <signal.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/mman.h>
  12. #include <sys/select.h>
  13. #include <pthread.h>
  14. #include <unistd.h>
  15. #include <sched.h>
  16. #include <errno.h>
  17.  
  18.  
  19. #define SW_PORTC_IO_BASE 0x01c20800
  20.  
  21. int main()
  22. {
  23.         unsigned int * pc;
  24.         int fd;
  25.         char * ptr;
  26.        
  27.         fd = open("/dev/mem", O_RDWR | O_SYNC);
  28.         if(fd < 0)
  29.         {
  30.                 perror("Unable to open /dev/mem");
  31.                 return(-1);
  32.         }
  33.        
  34.         pc = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, SW_PORTC_IO_BASE & 0xfffff000);
  35.        
  36.         if(pc == MAP_FAILED)
  37.         {
  38.                 perror("Unable to mmap file");
  39.                 return(-1);
  40.         }
  41.         ptr = (char *)pc + (SW_PORTC_IO_BASE & 0xfff);
  42.         *(unsigned int *)(ptr+0x00) = 0x11111111;
  43.         *(unsigned int *)(ptr+0x04) = 0x11111111;
  44.         *(unsigned int *)(ptr+0x08) = 0x11111111;
  45.         *(unsigned int *)(ptr+0x0c) = 0x11111111;
  46.         *(unsigned int *)(ptr+0x10) = 0xffffffff;
  47.         usleep(2000000);
  48.         *(unsigned int *)(ptr+0x10) = 0x00000000;
  49.         munmap(pc, 4096);
  50.         return 0;
  51. }
and there it is converted to Pascal:
Code: Pascal  [Select][+][-]
  1. program sunxi_lofasz;
  2.  
  3. uses baseunix, sysutils;
  4.  
  5. const SW_PORTC_IO_BASE = $01c20800;
  6.  
  7. var
  8.         pc: plongword;
  9.         fd: longint;
  10.         ptr: pbyte;
  11.  
  12. begin
  13.         fd := fpopen('/dev/mem', O_RDWR or O_SYNC);
  14.         if(fd < 0) then
  15.         begin
  16.                 writeln('Unable to open /dev/mem');
  17.                 halt;
  18.         end;
  19.        
  20.         pc := fpmmap(nil, 4096, PROT_READ or PROT_WRITE, MAP_SHARED, fd, SW_PORTC_IO_BASE and $fffff000);
  21.        
  22.         if(pc = MAP_FAILED) then
  23.         begin
  24.                 writeln('Unable to mmap file');
  25.                 halt;
  26.         end;
  27.         ptr := pbyte(pc + (SW_PORTC_IO_BASE and $fff));
  28.         plongword(ptr+$00)^ := $11111111;
  29.         plongword(ptr+$04)^ := $11111111;
  30.         plongword(ptr+$08)^ := $11111111;
  31.         plongword(ptr+$0c)^ := $11111111;
  32.         plongword(ptr+$10)^ := $ffffffff;
  33.         sleep(2000);
  34.         plongword(ptr+$10)^ := $00000000;
  35.         fpmunmap(pc, 4096);
  36. end.
  37.  
The C code works perfectly on Orange Pi, lits up the LEDs and then shuts them off. The Pascal on the other hand halts at the fpmmap, that it cannot mmap the file. If i change the last parameter from
Code: Pascal  [Select][+][-]
  1. SW_PORTC_IO_BASE and $fffff000
to
Code: Pascal  [Select][+][-]
  1. SW_PORTC_IO_BASE shr 12
then the code does not throws any error, but still don't works.

What can be this?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8810
  • FPC developer.
Re: C code works, same code in FreePascal does not
« Reply #1 on: August 22, 2018, 09:12:29 pm »
So use strace and find the two differences? How hard can it be?

TCH

  • Full Member
  • ***
  • Posts: 119
Re: C code works, same code in FreePascal does not
« Reply #2 on: August 22, 2018, 09:30:46 pm »
C:
Code: Text  [Select][+][-]
  1. execve("./sunxi_lofasz", ["./sunxi_lofasz"], [/* 54 vars */]) = 0
  2. brk(NULL)                               = 0x9c9000
  3. uname({sysname="Linux", nodename="orangepipcplus", ...}) = 0
  4. access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
  5. access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
  6. open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
  7. fstat64(3, {st_mode=S_IFREG|0644, st_size=93978, ...}) = 0
  8. mmap2(NULL, 93978, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6f71000
  9. close(3)                                = 0
  10. access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
  11. open("/lib/arm-linux-gnueabihf/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
  12. read(3, "\177ELF\1\1\1\3\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\331i\1\0004\0\0\0"..., 512) = 512
  13. lseek(3, 894128, SEEK_SET)              = 894128
  14. read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 2960) = 2960
  15. lseek(3, 888324, SEEK_SET)              = 888324
  16. read(3, "A2\0\0\0aeabi\0\1(\0\0\0\0057-A\0\6\n\7A\10\1\t\2\n\3\f"..., 51) = 51
  17. fstat64(3, {st_mode=S_IFREG|0755, st_size=897088, ...}) = 0
  18. mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f70000
  19. mmap2(NULL, 963928, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6e84000
  20. mprotect(0xb6f5a000, 65536, PROT_NONE)  = 0
  21. mmap2(0xb6f6a000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xd6000) = 0xb6f6a000
  22. mmap2(0xb6f6d000, 9560, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6f6d000
  23. close(3)                                = 0
  24. mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6fae000
  25. set_tls(0xb6fae4c0, 0xb6faeb98, 0xb6fb0050, 0xb6fae4c0, 0xb6fb0050) = 0
  26. mprotect(0xb6f6a000, 8192, PROT_READ)   = 0
  27. mprotect(0x20000, 4096, PROT_READ)      = 0
  28. mprotect(0xb6faf000, 4096, PROT_READ)   = 0
  29. munmap(0xb6f71000, 93978)               = 0
  30. open("/dev/mem", O_RDWR|O_SYNC)         = 3
  31. mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0x1c20000) = 0xb6fad000
  32. nanosleep({2, 0}, NULL)                 = 0
  33. munmap(0xb6fad000, 4096)                = 0
  34. exit_group(0)                           = ?
  35. +++ exited with 0 +++
  36.  
Pascal:
Code: Text  [Select][+][-]
  1. execve("./sunxi_lofasz", ["./sunxi_lofasz"], [/* 54 vars */]) = 0
  2. ugetrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0
  3. rt_sigaction(SIGFPE, {0x3b140, [], SA_RESTORER|SA_SIGINFO, 0x19020}, {SIG_DFL, [], 0}, 8) = 0
  4. rt_sigaction(SIGSEGV, {0x3b140, [], SA_RESTORER|SA_SIGINFO, 0x19020}, {SIG_DFL, [], 0}, 8) = 0
  5. rt_sigaction(SIGBUS, {0x3b140, [], SA_RESTORER|SA_SIGINFO, 0x19020}, {SIG_DFL, [], 0}, 8) = 0
  6. rt_sigaction(SIGILL, {0x3b140, [], SA_RESTORER|SA_SIGINFO, 0x19020}, {SIG_DFL, [], 0}, 8) = 0
  7. ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
  8. ioctl(2, TCGETS, 0xbed12160)            = -1 ENOTTY (Inappropriate ioctl for device)
  9. ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
  10. ioctl(2, TCGETS, 0xbed12160)            = -1 ENOTTY (Inappropriate ioctl for device)
  11. readlink("/proc/self/exe", "/root/sunxi_lofasz", 255) = 18
  12. open("/etc/timezone", O_RDONLY|O_LARGEFILE) = 3
  13. read(3, "Europe/Budapest\n", 255)       = 16
  14. close(3)                                = 0
  15. open("/usr/share/zoneinfo/Europe/Budapest", O_RDONLY|O_LARGEFILE) = 3
  16. read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\7\0\0\0\7\0\0\0\0"..., 2048) = 2048
  17. mmap2(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f7b000
  18. mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f73000
  19. mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f6b000
  20. mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f63000
  21. close(3)                                = 0
  22. gettimeofday({1534965612, 974462}, {tz_minuteswest=-60, tz_dsttime=0}) = 0
  23. open("/dev/mem", O_RDWR|O_DSYNC|O_LARGEFILE) = 3
  24. mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0x1c20000) = 0xb6f62000
  25. nanosleep({2, 0}, 0xbed12288)           = 0
  26. munmap(0xb6f62000, 4096)                = 0
  27. --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x11111119} ---
  28. rt_sigprocmask(SIG_UNBLOCK, [SEGV], NULL, 8) = 0
  29. --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x11111119} ---
  30. rt_sigprocmask(SIG_UNBLOCK, [SEGV], NULL, 8) = 0
  31. mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f5b000
  32. --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x11111119} ---
  33. rt_sigprocmask(SIG_UNBLOCK, [SEGV], NULL, 8) = 0
  34. munmap(0xb6f6b000, 32768)               = 0
  35. munmap(0xb6f73000, 32768)               = 0
  36. munmap(0xb6f7b000, 262144)              = 0
  37. exit_group(217)                         = ?
  38. +++ exited with 217 +++
  39.  
The only relevant difference i see in the logs is that C does
Code: [Select]
open("/dev/mem", O_RDWR|O_SYNC) and Pascal does
Code: [Select]
open("/dev/mem", O_RDWR|O_DSYNC|O_LARGEFILE)Could that be the problem? And how is it possible? I gave exactly the same parameters to in the Pascal program to open.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2520
    • havefunsoft.com
Re: C code works, same code in FreePascal does not
« Reply #3 on: August 22, 2018, 10:12:29 pm »
And how is it possible? I gave exactly the same parameters to in the Pascal program to open.
RTL fpopen code looks like this:
Code: Pascal  [Select][+][-]
  1. function Fpopen(path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  2.  
  3. Begin
  4.  Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags or O_LARGEFILE),TSysParam(mode));
  5. End;
  6.  

you might want to try it with your own version:
Code: Pascal  [Select][+][-]
  1. program sunxi_lofasz;
  2.  
  3. uses baseunix, sysutils
  4.   ,oldlinux; // this is needed for syscall_nr_open
  5.  
  6. ...
  7.  
  8. function Fpopen2(path: pchar; flags : cint; mode: mode_t):cint;
  9. Begin
  10.  Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags),TSysParam(mode));
  11. End;
  12. ...
  13. var
  14.         pc: plongword;
  15.         fd: longint;
  16.         ptr: pbyte;
  17.  
  18. begin
  19.         fd := fpopen2('/dev/mem', O_RDWR or O_SYNC);
  20.  
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

TCH

  • Full Member
  • ***
  • Posts: 119
Re: C code works, same code in FreePascal does not
« Reply #4 on: August 22, 2018, 10:40:27 pm »
Compiler cannot find "oldlinux". It's there in /usr/share/fpcsrc/3.0.2/rtl/linux/, but no avail.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8810
  • FPC developer.
Re: C code works, same code in FreePascal does not
« Reply #5 on: August 22, 2018, 10:45:20 pm »
oldlinux is a 23 year old unit that has been deprecated 15 years.

One of the reasons was that it was not portable to non x86 :-)

Best to copy the constants from sysnr.inc.

TCH

  • Full Member
  • ***
  • Posts: 119
Re: C code works, same code in FreePascal does not
« Reply #6 on: August 22, 2018, 10:56:29 pm »
Something is not right. This fpopen has three parameters, but i only used two and the compiler accepted it. Now that i copied the constants and used this fpopen2, it does need the third parameter.

Edit: I found it in oscdecl.inc: function  real_FpOpen(path: pchar; flags : cint):cint; varargs; cdecl; external 'c' name 'open';

Still, does not work.
« Last Edit: August 22, 2018, 11:01:53 pm by TCH »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8810
  • FPC developer.
Re: C code works, same code in FreePascal does not
« Reply #7 on: August 22, 2018, 11:09:23 pm »
The 3rd parameter is irrelevant (from man(2)):


The third parameter...
Quote
  mode specifies the permissions to use in case a new file is cre-
              ated.  This argument must be supplied when O_CREAT or  O_TMPFILE
              is specified in flags; if neither O_CREAT nor O_TMPFILE is spec-
              ified, then mode is ignored.

IOW the two parameter form is an extra shortcut, not a different function.

TCH

  • Full Member
  • ***
  • Posts: 119
Re: C code works, same code in FreePascal does not
« Reply #8 on: August 22, 2018, 11:14:05 pm »
Used the following parts:
Code: Pascal  [Select][+][-]
  1. const
  2. {$ifdef FPC_ABI_EABI}
  3.   syscall_nr_base = $0;
  4. {$else FPC_ABI_EABI}
  5.    syscall_nr_base = $900000;
  6. {$endif FPC_ABI_EABI}
  7.  
  8.   syscall_nr_open                       =  syscall_nr_base+ 5;
  9.  
  10. type
  11.         TSysResult = longint;
  12.         TSysParam  = Longint;
  13.  
  14. function Do_SysCall(sysnr,param1,param2,param3:TSysParam):TSysResult; external name 'FPC_SYSCALL3';
  15.  
  16. function Fpopen2(path: pchar; flags : cint; mode: mode_t):cint;
  17. Begin
  18.         Fpopen2 := do_syscall(5, TSysParam(path), TSysParam(flags), TSysParam(mode));
  19. End;
and calling it
Code: Pascal  [Select][+][-]
  1. fd := fpopen2('/dev/mem', O_RDWR or O_SYNC, 0);
still results in mmap failure.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2520
    • havefunsoft.com
Re: C code works, same code in FreePascal does not
« Reply #9 on: August 22, 2018, 11:31:00 pm »
oldlinux is a 23 year old unit that has been deprecated 15 years.
yeah. but linux rtl is still sys call ids from oldlinux unit.
I'd think Orange Pi is running linux anyway.

still results in mmap failure.
do you have mmap used from libc as well?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

TCH

  • Full Member
  • ***
  • Posts: 119
Re: C code works, same code in FreePascal does not
« Reply #10 on: August 22, 2018, 11:38:04 pm »
still results in mmap failure.
do you have mmap used from libc as well?
Yeah, i tried. No use, still cannot do it. But perhaps i did not use it correctly from Pascal.

Meanwhile i've checked the assembly.

C:
Code: ASM  [Select][+][-]
  1.         .arch armv7-a
  2.         .eabi_attribute 28, 1
  3.         .fpu vfpv3-d16
  4.         .eabi_attribute 20, 1
  5.         .eabi_attribute 21, 1
  6.         .eabi_attribute 23, 3
  7.         .eabi_attribute 24, 1
  8.         .eabi_attribute 25, 1
  9.         .eabi_attribute 26, 2
  10.         .eabi_attribute 30, 6
  11.         .eabi_attribute 34, 1
  12.         .eabi_attribute 18, 4
  13.         .file   "sunxi_lofasz.c"
  14.         .section        .rodata
  15.         .align  2
  16. .LC0:
  17.         .ascii  "/dev/mem\000"
  18.         .align  2
  19. .LC1:
  20.         .ascii  "Unable to open /dev/mem\000"
  21.         .align  2
  22. .LC2:
  23.         .ascii  "Unable to mmap file\000"
  24.         .text
  25.         .align  2
  26.         .global main
  27.         .syntax unified
  28.         .thumb
  29.         .thumb_func
  30.         .type   main, %function
  31. main:
  32.         @ args = 0, pretend = 0, frame = 16
  33.         @ frame_needed = 1, uses_anonymous_args = 0
  34.         push    {r7, lr}
  35.         sub     sp, sp, #24
  36.         add     r7, sp, #8
  37.         movw    r1, #4098
  38.         movt    r1, 16
  39.         movw    r0, #:lower16:.LC0
  40.         movt    r0, #:upper16:.LC0
  41.         bl      open
  42.         str     r0, [r7, #4]
  43.         ldr     r3, [r7, #4]
  44.         cmp     r3, #0
  45.         bge     .L2
  46.         movw    r0, #:lower16:.LC1
  47.         movt    r0, #:upper16:.LC1
  48.         bl      perror
  49.         mov     r3, #-1
  50.         b       .L3
  51. .L2:
  52.         mov     r3, #29491200
  53.         str     r3, [sp, #4]
  54.         ldr     r3, [r7, #4]
  55.         str     r3, [sp]
  56.         movs    r3, #1
  57.         movs    r2, #3
  58.         mov     r1, #4096
  59.         movs    r0, #0
  60.         bl      mmap
  61.         str     r0, [r7, #8]
  62.         ldr     r3, [r7, #8]
  63.         cmp     r3, #-1
  64.         bne     .L4
  65.         movw    r0, #:lower16:.LC2
  66.         movt    r0, #:upper16:.LC2
  67.         bl      perror
  68.         mov     r3, #-1
  69.         b       .L3
  70. .L4:
  71.         ldr     r3, [r7, #8]
  72.         add     r3, r3, #2048
  73.         str     r3, [r7, #12]
  74.         ldr     r3, [r7, #12]
  75.         mov     r2, #286331153
  76.         str     r2, [r3]
  77.         ldr     r3, [r7, #12]
  78.         adds    r3, r3, #4
  79.         mov     r2, #286331153
  80.         str     r2, [r3]
  81.         ldr     r3, [r7, #12]
  82.         adds    r3, r3, #8
  83.         mov     r2, #286331153
  84.         str     r2, [r3]
  85.         ldr     r3, [r7, #12]
  86.         adds    r3, r3, #12
  87.         mov     r2, #286331153
  88.         str     r2, [r3]
  89.         ldr     r3, [r7, #12]
  90.         adds    r3, r3, #16
  91.         mov     r2, #-1
  92.         str     r2, [r3]
  93.         movw    r0, #33920
  94.         movt    r0, 30
  95.         bl      usleep
  96.         ldr     r3, [r7, #12]
  97.         adds    r3, r3, #16
  98.         movs    r2, #0
  99.         str     r2, [r3]
  100.         mov     r1, #4096
  101.         ldr     r0, [r7, #8]
  102.         bl      munmap
  103.         movs    r3, #0
  104. .L3:
  105.         mov     r0, r3
  106.         adds    r7, r7, #16
  107.         mov     sp, r7
  108.         @ sp needed
  109.         pop     {r7, pc}
  110.         .size   main, .-main
  111.         .ident  "GCC: (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609"
  112.         .section        .note.GNU-stack,"",%progbits
Pascal:
Code: ASM  [Select][+][-]
  1.         .file "sunxi_lofasz.pas"
  2. # Begin asmlist al_procedures
  3.  
  4. .section .text.n_main
  5.         .balign 4
  6. .globl  PASCALMAIN
  7.         .type   PASCALMAIN,#function
  8. PASCALMAIN:
  9. .globl  main
  10.         .type   main,#function
  11. main:
  12.         mov     r12,r13
  13.         stmfd   r13!,{r3,r4,r11,r12,r14,r15}
  14.         sub     r11,r12,#4
  15.         sub     r13,r13,#64
  16.         blx     FPC_INITIALIZEUNITS
  17.         mov     r1,#2
  18.         orr     r1,r1,#4096
  19.         ldr     r0,.Lj9
  20.         blx     BASEUNIX_$$_FPOPEN$SHORTSTRING$LONGINT$$LONGINT
  21.         ldr     r1,.Lj10
  22.         str     r0,[r1]
  23.         ldr     r0,.Lj10
  24.         ldr     r0,[r0]
  25.         cmp     r0,#0
  26.         blt     .Lj11
  27.         b       .Lj12
  28. .Lj11:
  29.         blx     fpc_get_output
  30.         mov     r4,r0
  31.         ldr     r2,.Lj18
  32.         mov     r1,r4
  33.         mov     r0,#0
  34.         blx     fpc_write_text_shortstr
  35.         blx     FPC_IOCHECK
  36.         mov     r0,r4
  37.         blx     fpc_writeln_end
  38.         blx     FPC_IOCHECK
  39.         mov     r0,#0
  40.         blx     SYSTEM_$$_HALT$LONGINT
  41. .Lj12:
  42.         ldr     r0,.Lj10
  43.         ldr     r0,[r0]
  44.         str     r0,[r13]
  45.         mov     r0,#0
  46.         str     r0,[r13, #12]
  47.         mov     r0,#16777216
  48.         orr     r0,r0,#12713984
  49.         str     r0,[r13, #8]
  50.         mov     r3,#1
  51.         mov     r2,#3
  52.         mov     r1,#4096
  53.         mov     r0,#0
  54.         blx     FPC_SYSC_MMAP
  55.         ldr     r1,.Lj42
  56.         str     r0,[r1]
  57.         ldr     r0,.Lj42
  58.         ldr     r0,[r0]
  59.         mvn     r1,#0
  60.         cmp     r0,r1
  61.         beq     .Lj43
  62.         b       .Lj44
  63. .Lj43:
  64.         blx     fpc_get_output
  65.         mov     r4,r0
  66.         ldr     r2,.Lj50
  67.         mov     r1,r4
  68.         mov     r0,#0
  69.         blx     fpc_write_text_shortstr
  70.         blx     FPC_IOCHECK
  71.         mov     r0,r4
  72.         blx     fpc_writeln_end
  73.         blx     FPC_IOCHECK
  74.         mov     r0,#0
  75.         blx     SYSTEM_$$_HALT$LONGINT
  76. .Lj44:
  77.         ldr     r0,.Lj42
  78.         ldr     r0,[r0]
  79.         add     r0,r0,#8192
  80.         ldr     r1,.Lj62
  81.         str     r0,[r1]
  82.         ldr     r0,.Lj62
  83.         ldr     r0,[r0]
  84.         ldr     r1,.Lj66
  85.         str     r1,[r0]
  86.         ldr     r0,.Lj62
  87.         ldr     r1,[r0]
  88.         ldr     r0,.Lj66
  89.         str     r0,[r1, #4]
  90.         ldr     r0,.Lj62
  91.         ldr     r1,[r0]
  92.         ldr     r0,.Lj66
  93.         str     r0,[r1, #8]
  94.         ldr     r0,.Lj62
  95.         ldr     r0,[r0]
  96.         ldr     r1,.Lj66
  97.         str     r1,[r0, #12]
  98.         ldr     r0,.Lj62
  99.         ldr     r0,[r0]
  100.         mvn     r1,#0
  101.         str     r1,[r0, #16]
  102.         mov     r0,#2000
  103.         blx     SYSUTILS_$$_SLEEP$LONGWORD
  104.         ldr     r0,.Lj62
  105.         ldr     r0,[r0]
  106.         mov     r1,#0
  107.         str     r1,[r0, #16]
  108.         ldr     r0,.Lj42
  109.         ldr     r0,[r0]
  110.         mov     r1,#4096
  111.         blx     FPC_SYSC_MUNMAP
  112.         blx     FPC_DO_EXIT
  113.         ldmea   r11,{r3,r4,r11,r13,r15}
  114. .Lj9:
  115.         .long   _$SUNXI_LOFASZ$_Ld1
  116. .Lj10:
  117.         .long   U_$P$SUNXI_LOFASZ_$$_FD
  118. .Lj18:
  119.         .long   _$SUNXI_LOFASZ$_Ld2
  120. .Lj42:
  121.         .long   U_$P$SUNXI_LOFASZ_$$_PC
  122. .Lj50:
  123.         .long   _$SUNXI_LOFASZ$_Ld3
  124. .Lj62:
  125.         .long   U_$P$SUNXI_LOFASZ_$$_PTR
  126. .Lj66:
  127.         .long   286331153
  128. .Le0:
  129.         .size   main, .Le0 - main
  130.  
  131. .section .text
  132. # End asmlist al_procedures
  133. # Begin asmlist al_globals
  134.  
  135. .section .bss
  136.         .balign 4
  137.         .type U_$P$SUNXI_LOFASZ_$$_PC,%object
  138.         .size U_$P$SUNXI_LOFASZ_$$_PC,4
  139. U_$P$SUNXI_LOFASZ_$$_PC:
  140.         .zero 4
  141.  
  142. .section .bss
  143.         .balign 4
  144.         .type U_$P$SUNXI_LOFASZ_$$_FD,%object
  145.         .size U_$P$SUNXI_LOFASZ_$$_FD,4
  146. U_$P$SUNXI_LOFASZ_$$_FD:
  147.         .zero 4
  148.  
  149. .section .bss
  150.         .balign 4
  151.         .type U_$P$SUNXI_LOFASZ_$$_PTR,%object
  152.         .size U_$P$SUNXI_LOFASZ_$$_PTR,4
  153. U_$P$SUNXI_LOFASZ_$$_PTR:
  154.         .zero 4
  155.  
  156. .section .data.n_INITFINAL
  157.         .balign 4
  158. .globl  INITFINAL
  159.         .type   INITFINAL,#object
  160. INITFINAL:
  161.         .long   5,0
  162.         .long   INIT$_$SYSTEM
  163.         .long   0,0
  164.         .long   FINALIZE$_$OBJPAS
  165.         .long   INIT$_$UNIX
  166.         .long   FINALIZE$_$UNIX
  167.         .long   SYSUTILS$_$TENCODING_$__$$_create
  168.         .long   SYSUTILS$_$TENCODING_$__$$_destroy
  169.         .long   INIT$_$SYSUTILS
  170.         .long   FINALIZE$_$SYSUTILS
  171. .Le1:
  172.         .size   INITFINAL, .Le1 - INITFINAL
  173.  
  174. .section .data.n_FPC_THREADVARTABLES
  175.         .balign 4
  176. .globl  FPC_THREADVARTABLES
  177.         .type   FPC_THREADVARTABLES,#object
  178. FPC_THREADVARTABLES:
  179.         .long   1
  180.         .long   THREADVARLIST_$SYSTEM
  181. .Le2:
  182.         .size   FPC_THREADVARTABLES, .Le2 - FPC_THREADVARTABLES
  183.  
  184. .section .data.n_FPC_RESOURCESTRINGTABLES
  185.         .balign 4
  186. .globl  FPC_RESOURCESTRINGTABLES
  187.         .type   FPC_RESOURCESTRINGTABLES,#object
  188. FPC_RESOURCESTRINGTABLES:
  189.         .long   1
  190.         .long   RESSTR_$SYSCONST_$$_START
  191.         .long   RESSTR_$SYSCONST_$$_END
  192. .Le3:
  193.         .size   FPC_RESOURCESTRINGTABLES, .Le3 - FPC_RESOURCESTRINGTABLES
  194.  
  195. .section .data.n_FPC_WIDEINITTABLES
  196.         .balign 4
  197. .globl  FPC_WIDEINITTABLES
  198.         .type   FPC_WIDEINITTABLES,#object
  199. FPC_WIDEINITTABLES:
  200.         .long   0
  201. .Le4:
  202.         .size   FPC_WIDEINITTABLES, .Le4 - FPC_WIDEINITTABLES
  203.  
  204. .section .data.n_FPC_RESSTRINITTABLES
  205.         .balign 4
  206. .globl  FPC_RESSTRINITTABLES
  207.         .type   FPC_RESSTRINITTABLES,#object
  208. FPC_RESSTRINITTABLES:
  209.         .long   0
  210. .Le5:
  211.         .size   FPC_RESSTRINITTABLES, .Le5 - FPC_RESSTRINITTABLES
  212.  
  213. .section .fpc.n_version
  214.         .balign 8
  215.         .ascii  "FPC 3.0.2 [2017/02/17] for arm - Linux"
  216.  
  217. .section .data.n___stklen
  218.         .balign 4
  219. .globl  __stklen
  220.         .type   __stklen,#object
  221. __stklen:
  222.         .long   8388608
  223.  
  224. .section .data.n___heapsize
  225.         .balign 4
  226. .globl  __heapsize
  227.         .type   __heapsize,#object
  228. __heapsize:
  229.         .long   0
  230.  
  231. .section .data.n___fpc_valgrind
  232. .globl  __fpc_valgrind
  233.         .type   __fpc_valgrind,#object
  234. __fpc_valgrind:
  235.         .byte   0
  236.  
  237. .section .data.n_FPC_RESLOCATION
  238.         .balign 4
  239. .globl  FPC_RESLOCATION
  240.         .type   FPC_RESLOCATION,#object
  241. FPC_RESLOCATION:
  242.         .long   0
  243. # End asmlist al_globals
  244. # Begin asmlist al_typedconsts
  245.  
  246. .section .rodata.n__$SUNXI_LOFASZ$_Ld1
  247.         .balign 4
  248. .globl  _$SUNXI_LOFASZ$_Ld1
  249. _$SUNXI_LOFASZ$_Ld1:
  250.         .ascii  "\010/dev/mem\000"
  251.  
  252. .section .rodata.n__$SUNXI_LOFASZ$_Ld2
  253.         .balign 4
  254. .globl  _$SUNXI_LOFASZ$_Ld2
  255. _$SUNXI_LOFASZ$_Ld2:
  256.         .ascii  "\027Unable to open /dev/mem\000"
  257.  
  258. .section .rodata.n__$SUNXI_LOFASZ$_Ld3
  259.         .balign 4
  260. .globl  _$SUNXI_LOFASZ$_Ld3
  261. _$SUNXI_LOFASZ$_Ld3:
  262.         .ascii  "\023Unable to mmap file\000"
  263. # End asmlist al_typedconsts
  264. .section .note.GNU-stack,"",%progbits
It's entirely different inside. Perhaps FPC_SYSC_MMAP is broken under ARM?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2520
    • havefunsoft.com
Re: C code works, same code in FreePascal does not
« Reply #11 on: August 23, 2018, 12:11:34 am »
just for fun
try to replace this line
Code: Pascal  [Select][+][-]
  1. ptr := pbyte(pc + (SW_PORTC_IO_BASE and $fff));
with
Code: Pascal  [Select][+][-]
  1. ptr := pbyte(pbyte(pc) + (SW_PORTC_IO_BASE and $fff)));
or a shorter version (if it compiles)
Code: Pascal  [Select][+][-]
  1. ptr := pbyte(pc) + (SW_PORTC_IO_BASE and $fff));

strace drop indicates some SIGSEGV. That makes me think that pointer arithmetic fails somewhere.
« Last Edit: August 23, 2018, 12:15:18 am by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

PascalDragon

  • Hero Member
  • *****
  • Posts: 2281
  • Compiler Developer
Re: C code works, same code in FreePascal does not
« Reply #12 on: August 23, 2018, 07:44:42 am »
oldlinux is a 23 year old unit that has been deprecated 15 years.
yeah. but linux rtl is still sys call ids from oldlinux unit.
I'd think Orange Pi is running linux anyway.
The Linux RTL pulls the syscall IDs from the sysnr.inc files located inside the platform specific directories cause the syscall IDs can and do differ between Linux platforms.

TCH

  • Full Member
  • ***
  • Posts: 119
Re: C code works, same code in FreePascal does not
« Reply #13 on: August 23, 2018, 09:45:37 am »
@skalogryz:

In the meanwhile my collegaue solved it, but he suggested exactly the same thing as you: it was a pointer offset flaw in my code. So, thanks to you too.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2520
    • havefunsoft.com
Re: C code works, same code in FreePascal does not
« Reply #14 on: August 23, 2018, 03:09:40 pm »
it's good to know that fpc is not that bad :)
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

 

TinyPortal © 2005-2018