瀏覽代碼

# revisions: 41710,41711,41733,41734,41735,41736,41737,41738,41739,41740,41741,41743,41744,41745,41746,41750,41751,41752,41753,41754,41755,41757,41758,41759

git-svn-id: branches/fixes_3_2@41988 -
marco 6 年之前
父節點
當前提交
abccef11e0

+ 5 - 2
.gitattributes

@@ -10111,10 +10111,11 @@ rtl/openbsd/errnostr.inc svneol=native#text/plain
 rtl/openbsd/i386/bsyscall.inc svneol=native#text/plain
 rtl/openbsd/i386/cprt0.as svneol=native#text/plain
 rtl/openbsd/i386/dllprt0.as svneol=native#text/plain
-rtl/openbsd/i386/openbsd_ident.as svneol=native#text/plain
+rtl/openbsd/i386/openbsd_ident.inc svneol=native#text/plain
 rtl/openbsd/i386/prt0.as svneol=native#text/plain
 rtl/openbsd/i386/si_c.inc svneol=native#text/plain
 rtl/openbsd/i386/si_dll.inc svneol=native#text/plain
+rtl/openbsd/i386/si_g.inc svneol=native#text/plain
 rtl/openbsd/i386/si_prc.inc svneol=native#text/plain
 rtl/openbsd/i386/sighnd.inc svneol=native#text/plain
 rtl/openbsd/osdefs.inc svneol=native#text/plain
@@ -10125,6 +10126,7 @@ rtl/openbsd/rtldefs.inc svneol=native#text/plain
 rtl/openbsd/setsysnr.inc svneol=native#text/plain
 rtl/openbsd/si_c.pp svneol=native#text/plain
 rtl/openbsd/si_dll.pp svneol=native#text/plain
+rtl/openbsd/si_g.pp svneol=native#text/plain
 rtl/openbsd/si_impl.inc svneol=native#text/plain
 rtl/openbsd/si_intf.inc svneol=native#text/plain
 rtl/openbsd/si_prc.pp svneol=native#text/plain
@@ -10147,10 +10149,11 @@ rtl/openbsd/x86_64/cprt0.as svneol=native#text/plain
 rtl/openbsd/x86_64/crt0.s svneol=native#text/plain
 rtl/openbsd/x86_64/dllprt0.as svneol=native#text/plain
 rtl/openbsd/x86_64/gprt0.as svneol=native#text/plain
-rtl/openbsd/x86_64/openbsd_ident.as svneol=native#text/plain
+rtl/openbsd/x86_64/openbsd_ident.inc svneol=native#text/plain
 rtl/openbsd/x86_64/prt0.as svneol=native#text/plain
 rtl/openbsd/x86_64/si_c.inc svneol=native#text/plain
 rtl/openbsd/x86_64/si_dll.inc svneol=native#text/plain
+rtl/openbsd/x86_64/si_g.inc svneol=native#text/plain
 rtl/openbsd/x86_64/si_prc.inc svneol=native#text/plain
 rtl/openbsd/x86_64/sighnd.inc svneol=native#text/plain
 rtl/os2/Makefile svneol=native#text/plain

+ 28 - 0
compiler/globals.pas

@@ -873,6 +873,30 @@ implementation
          end;
 
 {$endif mswindows}
+{$ifdef openbsd}
+       function GetOpenBSDLocalBase: ansistring;
+         var
+           envvalue: pchar;
+         begin
+           envvalue := GetEnvPChar('LOCALBASE');
+           if assigned(envvalue) then
+             Result:=envvalue
+           else
+             Result:='/usr/local';
+           FreeEnvPChar(envvalue);
+         end;
+       function GetOpenBSDX11Base: ansistring;
+         var
+           envvalue: pchar;
+         begin
+           envvalue := GetEnvPChar('X11BASE');
+           if assigned(envvalue) then
+             Result:=envvalue
+           else
+             Result:='/usr/X11R6';
+           FreeEnvPChar(envvalue);
+         end;
+{$endif openbsd}
        var
          envstr: string;
          envvalue: pchar;
@@ -905,6 +929,10 @@ implementation
          ReplaceSpecialFolder('$PROGRAM_FILES_COMMON',CSIDL_PROGRAM_FILES_COMMON);
          ReplaceSpecialFolder('$PROFILE',CSIDL_PROFILE);
 {$endif mswindows}
+{$ifdef openbsd}
+         Replace(s,'$OPENBSD_LOCALBASE',GetOpenBSDLocalBase);
+         Replace(s,'$OPENBSD_X11BASE',GetOpenBSDX11Base);
+{$endif openbsd}
          { Replace environment variables between dollar signs }
          i := pos('$',s);
          while i>0 do

+ 1 - 1
compiler/systems.pas

@@ -334,7 +334,7 @@ interface
        systems_indirect_entry_information = systems_darwin+[system_i386_win32,system_x86_64_win64,system_x86_64_linux];
 
        { all systems for which weak linking has been tested/is supported }
-       systems_weak_linking = systems_darwin + systems_solaris + systems_linux + systems_android;
+       systems_weak_linking = systems_darwin + systems_solaris + systems_linux + systems_android + systems_openbsd;
 
        systems_internal_sysinit = [system_i386_win32,system_x86_64_win64,
                                    system_i386_linux,system_powerpc64_linux,system_sparc64_linux,system_x86_64_linux,

+ 28 - 11
compiler/systems/t_bsd.pas

@@ -154,7 +154,7 @@ begin
      { Mac OS X doesn't have a /lib }
      LibrarySearchPath.AddPath(sysrootpath,'/usr/lib',true)
    else if target_info.system in systems_openbsd then
-     LibrarySearchPath.AddPath(sysrootpath,'/usr/lib;${X11BASE}/lib;${LOCALBASE}/lib',true)
+     LibrarySearchPath.AddPath(sysrootpath,'/usr/lib;$OPENBSD_X11BASE/lib;$OPENBSD_LOCALBASE/lib',true)
    else
      LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
 end;
@@ -173,8 +173,8 @@ begin
        begin
          if not(target_info.system in systems_darwin) then
            begin
-             ExeCmd[1]:='ld $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $CATRES $FILELIST';
-             DllCmd[1]:='ld $TARGET $EMUL $OPT -shared -L. -o $EXE $CATRES $FILELIST'
+             ExeCmd[1]:='ld $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP -L. -o $EXE $CATRES $FILELIST';
+             DllCmd[1]:='ld $TARGET $EMUL $OPT $MAP -shared -L. -o $EXE $CATRES $FILELIST'
            end
          else
            begin
@@ -193,22 +193,22 @@ begin
                programs with problems that require Valgrind will have more
                than 60KB of data (first 4KB of address space is always invalid)
              }
-               ExeCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST';
+               ExeCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST';
              if not(cs_gdb_valgrind in current_settings.globalswitches) then
                ExeCmd[1]:=ExeCmd[1]+' -pagezero_size 0x10000';
 {$else ndef cpu64bitaddr}
-             ExeCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST';
+             ExeCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST';
 {$endif ndef cpu64bitaddr}
              if (apptype<>app_bundle) then
-               DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS -dynamic -dylib -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
+               DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS $MAP -dynamic -dylib -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
              else
-               DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS -dynamic -bundle -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
+               DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS $MAP -dynamic -bundle -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
            end
        end
      else
        begin
-         ExeCmd[1]:='ld $TARGET $EMUL $OPT $DYNLINK $STATIC  $GCSECTIONS $STRIP -L. -o $EXE $RES';
-         DllCmd[1]:='ld $TARGET $EMUL $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
+         ExeCmd[1]:='ld $TARGET $EMUL $OPT $DYNLINK $STATIC  $GCSECTIONS $STRIP $MAP -L. -o $EXE $RES';
+         DllCmd[1]:='ld $TARGET $EMUL $OPT $INIT $FINI $SONAME $MAP -shared -L. -o $EXE $RES';
        end;
      if not(target_info.system in systems_darwin) then
        DllCmd[2]:='strip --strip-unneeded $EXE'
@@ -616,7 +616,10 @@ begin
    begin
      if librarysearchpath.FindFile('crti.o',false,s) then
       LinkRes.AddFileName(s);
-     if cs_create_pic in current_settings.moduleswitches then
+     if ((cs_create_pic in current_settings.moduleswitches) and
+         not (target_info.system in systems_openbsd)) or
+        (current_module.islibrary and
+         (target_info.system in systems_openbsd)) then
        begin
          if librarysearchpath.FindFile('crtbeginS.o',false,s) then
            LinkRes.AddFileName(s);
@@ -740,7 +743,10 @@ begin
   if linklibc and
      not IsDarwin Then
    begin
-     if cs_create_pic in current_settings.moduleswitches then
+     if ((cs_create_pic in current_settings.moduleswitches) and
+         not (target_info.system in systems_openbsd)) or
+        (current_module.islibrary and
+         (target_info.system in systems_openbsd)) then
        Fl1:=librarysearchpath.FindFile('crtendS.o',false,s1)
      else
        Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
@@ -767,6 +773,7 @@ function TLinkerBSD.MakeExecutable:boolean;
 var
   binstr,
   cmdstr,
+  mapstr,
   targetstr,
   emulstr,
   extdbgbinstr,
@@ -788,6 +795,9 @@ begin
   DynLinkStr:='';
   GCSectionsStr:='';
   linkscript:=nil;
+  mapstr:='';
+  if (cs_link_map in current_settings.globalswitches) then
+    mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
   { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
     to avoid creation of a i386:x86_64 arch binary }
 
@@ -857,6 +867,7 @@ begin
   Replace(cmdstr,'$OPT',Info.ExtraOptions);
   Replace(cmdstr,'$TARGET',targetstr);
   Replace(cmdstr,'$EMUL',EmulStr);
+  Replace(cmdstr,'$MAP',mapstr);
   Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
   Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
   if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
@@ -934,6 +945,7 @@ var
   linkscript: TAsmScript;
   binstr,
   cmdstr,
+  mapstr,
   targetstr,
   emulstr,
   extdbgbinstr,
@@ -944,6 +956,7 @@ var
 begin
   MakeSharedLibrary:=false;
   GCSectionsStr:='';
+  mapstr:='';
   linkscript:=nil;
   if not(cs_link_nolink in current_settings.globalswitches) then
    Message1(exec_i_linking,current_module.sharedlibfilename);
@@ -959,6 +972,9 @@ begin
     else
       GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
 
+  if (cs_link_map in current_settings.globalswitches) then
+    mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
+
   { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
     to avoid creation of a i386:x86_64 arch binary }
 
@@ -997,6 +1013,7 @@ begin
   Replace(cmdstr,'$FINI',FiniStr);
   Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
   Replace(cmdstr,'$SONAME',SoNameStr);
+  Replace(cmdstr,'$MAP',mapstr);
   if (target_info.system in systems_darwin) then
     Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
   BinStr:=FindUtil(utilsprefix+BinStr);

+ 8 - 9
rtl/openbsd/Makefile

@@ -355,14 +355,14 @@ override FPCOPT+=-Ur
 endif
 CPU_UNITS=
 SYSINIT_UNITS=
-LOADERS=prt0 cprt0 dllprt0 openbsd_ident
+LOADERS=prt0 cprt0 dllprt0
 ifeq ($(ARCH),x86_64)
 CPU_UNITS=x86 ports cpu
-SYSINIT_UNITS=si_prc si_c si_dll
+SYSINIT_UNITS=si_prc si_c si_dll si_g
 endif
 ifeq ($(ARCH),i386)
 CPU_UNITS=x86 ports cpu mmx
-SYSINIT_UNITS=si_prc si_c si_dll
+SYSINIT_UNITS=si_prc si_c si_dll si_g
 endif
 OBJPASDIR=$(RTL)/objpas
 GRAPHDIR=$(INC)/graph
@@ -3014,7 +3014,6 @@ ifdef CLEAN_FILES
 	-$(DEL) $(CLEAN_FILES)
 endif
 	-$(DELTREE) units
-	-$(DELTREE) bin
 	-$(DEL) *$(OEXT) *$(PPUEXT) *$(RSTEXT) *$(ASMEXT) *$(STATICLIBEXT) *$(SHAREDLIBEXT) *$(PPLEXT)
 ifneq ($(PPUEXT),.ppu)
 	-$(DEL) *.o *.ppu *.a
@@ -3176,13 +3175,13 @@ cprt0$(OEXT) : $(CPU_TARGET)/cprt0.as
 	$(AS) -o $(UNITTARGETDIRPREFIX)cprt0$(OEXT) $(CPU_TARGET)/cprt0.as
 dllprt0$(OEXT) : $(CPU_TARGET)/dllprt0.as
 	$(AS) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) $(CPU_TARGET)/dllprt0.as
-openbsd_ident$(OEXT) : $(CPU_TARGET)/openbsd_ident.as
-	$(AS) -o $(UNITTARGETDIRPREFIX)openbsd_ident$(OEXT) $(CPU_TARGET)/openbsd_ident.as
-si_prc$(PPUEXT) : si_prc.pp si_intf.inc $(ARCH)/si_prc.inc $(SYSTEMUNIT)$(PPUEXT)
+si_prc$(PPUEXT) : si_prc.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_prc.inc $(SYSTEMUNIT)$(PPUEXT)
 	$(COMPILER) $<
-si_c$(PPUEXT) : si_c.pp si_intf.inc $(ARCH)/si_c.inc $(SYSTEMUNIT)$(PPUEXT)
+si_c$(PPUEXT) : si_c.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_c.inc $(SYSTEMUNIT)$(PPUEXT)
 	$(COMPILER) $<
-si_dll$(PPUEXT) : si_dll.pp si_intf.inc $(ARCH)/si_dll.inc $(SYSTEMUNIT)$(PPUEXT)
+si_dll$(PPUEXT) : si_dll.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_dll.inc $(SYSTEMUNIT)$(PPUEXT)
+	$(COMPILER) $<
+si_g$(PPUEXT) : si_g.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_g.inc $(SYSTEMUNIT)$(PPUEXT)
 	$(COMPILER) $<
 $(SYSTEMUNIT)$(PPUEXT) : $(BSDINC)/$(SYSTEMUNIT).pp sysconst.inc systypes.inc syscalls.inc $(SYSDEPS)
 	$(COMPILER) $(FPC_SYSTEM_OPT) -Us -Sg $(BSDINC)/$(SYSTEMUNIT).pp

+ 9 - 9
rtl/openbsd/Makefile.fpc

@@ -85,15 +85,15 @@ endif
 CPU_UNITS=
 SYSINIT_UNITS=
 
-LOADERS=prt0 cprt0 dllprt0 openbsd_ident
+LOADERS=prt0 cprt0 dllprt0
 
 ifeq ($(ARCH),x86_64)
 CPU_UNITS=x86 ports cpu
-SYSINIT_UNITS=si_prc si_c si_dll
+SYSINIT_UNITS=si_prc si_c si_dll si_g
 endif
 ifeq ($(ARCH),i386)
 CPU_UNITS=x86 ports cpu mmx
-SYSINIT_UNITS=si_prc si_c si_dll
+SYSINIT_UNITS=si_prc si_c si_dll si_g
 endif
 
 # Paths
@@ -141,20 +141,20 @@ cprt0$(OEXT) : $(CPU_TARGET)/cprt0.as
 dllprt0$(OEXT) : $(CPU_TARGET)/dllprt0.as
         $(AS) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) $(CPU_TARGET)/dllprt0.as
 
-openbsd_ident$(OEXT) : $(CPU_TARGET)/openbsd_ident.as
-        $(AS) -o $(UNITTARGETDIRPREFIX)openbsd_ident$(OEXT) $(CPU_TARGET)/openbsd_ident.as
-
 
 #
 # $(SYSINIT_UNITS) Units
 #
-si_prc$(PPUEXT) : si_prc.pp si_intf.inc $(ARCH)/si_prc.inc $(SYSTEMUNIT)$(PPUEXT)
+si_prc$(PPUEXT) : si_prc.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_prc.inc $(SYSTEMUNIT)$(PPUEXT)
+	$(COMPILER) $<
+
+si_c$(PPUEXT) : si_c.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_c.inc $(SYSTEMUNIT)$(PPUEXT)
 	$(COMPILER) $<
 
-si_c$(PPUEXT) : si_c.pp si_intf.inc $(ARCH)/si_c.inc $(SYSTEMUNIT)$(PPUEXT)
+si_dll$(PPUEXT) : si_dll.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_dll.inc $(SYSTEMUNIT)$(PPUEXT)
 	$(COMPILER) $<
 
-si_dll$(PPUEXT) : si_dll.pp si_intf.inc $(ARCH)/si_dll.inc $(SYSTEMUNIT)$(PPUEXT)
+si_g$(PPUEXT) : si_g.pp si_intf.inc si_impl.inc $(ARCH)/openbsd_ident.inc $(ARCH)/si_g.inc $(SYSTEMUNIT)$(PPUEXT)
 	$(COMPILER) $<
 
 

+ 0 - 7
rtl/openbsd/i386/openbsd_ident.as

@@ -1,7 +0,0 @@
-	.section ".note.openbsd.ident", "a"
-	.p2align 2
-	.long	8
-	.long	4
-	.long	1
-	.ascii "OpenBSD\0"
-	.long	0

+ 14 - 0
rtl/openbsd/i386/openbsd_ident.inc

@@ -0,0 +1,14 @@
+{$asmmode att}
+
+procedure OpenBSDIdentTag;nostackframe;assembler;
+  asm
+    .section ".note.openbsd.ident", "a"
+    .p2align 2
+    .long    8
+    .long    4
+    .long    1
+    .asciz   "OpenBSD"
+    .long    0
+
+    .text
+  end;

+ 16 - 0
rtl/openbsd/i386/si_g.inc

@@ -0,0 +1,16 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2019 by Free Pascal development team
+
+    This file implements parts of the startup code for OpenBSD
+    programs, compiled with gprof support.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+

+ 28 - 0
rtl/openbsd/si_g.pp

@@ -0,0 +1,28 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2019 by Free Pascal development team
+
+    This file implements parts of the startup code for OpenBSD
+    programs, compiled with gprof support.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+unit si_g;
+
+interface
+
+{$i si_intf.inc}
+
+implementation
+
+{$i si_impl.inc}
+{$i si_g.inc}
+
+end.

+ 3 - 1
rtl/openbsd/si_impl.inc

@@ -12,7 +12,9 @@
 
  **********************************************************************}
 
-{$LINK openbsd_ident}
+{$IFNDEF VER3_0}
+  {$I openbsd_ident.inc}
+{$ENDIF VER3_0}
 
 procedure PascalMain; external name 'PASCALMAIN';
 

+ 10 - 0
rtl/openbsd/si_intf.inc

@@ -17,6 +17,16 @@
 {$SMARTLINK OFF}
 {$GOTO ON}
 
+type
+  cint = LongInt;
+{$if defined(cpu64)}
+  culong = QWord;
+{$else}
+  culong = LongWord;
+{$endif}
+  u_long = culong;
+  TCdeclProcedure = procedure; cdecl;
+
 var
   operatingsystem_parameter_envp: ppchar; public name 'operatingsystem_parameter_envp';
   operatingsystem_parameter_argc: longint; public name 'operatingsystem_parameter_argc';

+ 0 - 7
rtl/openbsd/x86_64/openbsd_ident.as

@@ -1,7 +0,0 @@
-	.section ".note.openbsd.ident", "a"
-	.p2align 2
-	.long	8
-	.long	4
-	.long	1
-	.ascii "OpenBSD\0"
-	.long	0

+ 14 - 0
rtl/openbsd/x86_64/openbsd_ident.inc

@@ -0,0 +1,14 @@
+{$asmmode gas}
+
+procedure OpenBSDIdentTag;nostackframe;assembler;
+  asm
+    .section ".note.openbsd.ident", "a"
+    .p2align 2
+    .long    8
+    .long    4
+    .long    1
+    .asciz   "OpenBSD"
+    .long    0
+
+    .text
+  end;

+ 52 - 131
rtl/openbsd/x86_64/si_c.inc

@@ -16,16 +16,17 @@
 
 {$asmmode gas}
 
-var
+{var
   _etext: Byte; external name '_etext';
-  _eprol: Byte; external name '_eprol';
+  _eprol: Byte; external name '_eprol';}
 
-procedure _mcleanup; external name '_mcleanup';
-procedure atexit; external name 'atexit';
-procedure monstartup; external name 'monstartup';
-procedure _init; external name '_init';
+{procedure _mcleanup; cdecl; external name '_mcleanup';
+function atexit(proc: TCdeclProcedure): cint; cdecl; external name 'atexit';
+procedure monstartup(lowpc, highpc: u_long); cdecl; external name 'monstartup';}
+procedure __init; cdecl; external name '__init';
+procedure c_exit(exit_code: cint); cdecl; noreturn; external name 'exit';
 
-procedure _FPC_proc___start; forward;
+procedure _FPC_proc___start(argc: LongInt; argv: PPChar; envp: Pointer; para1, para2, para3: QWord); forward;
 
 procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public name '__start';
   asm
@@ -41,136 +42,56 @@ procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public
     jmp     _FPC_proc___start
   end;
 
-procedure _FPC_proc_haltproc; forward;
+procedure _FPC_proc_haltproc; noreturn; forward;
 function _strrchr(str: PChar; character: LongInt): PChar; forward;
 
-procedure _FPC_proc___start; assembler; nostackframe; public name '___start';
-  asm
-    pushq   %rbp
-    movq    %rsp, %rbp
-    subq    $64, %rsp
-    movl    %edi, -20(%rbp)
-    movq    %rsi, -32(%rbp)
-    movq    %rdx, -40(%rbp)
-    movq    %rcx, -48(%rbp)
-    movq    %r8, -56(%rbp)
-    movq    %r9, -64(%rbp)
-    movq    -40(%rbp), %rax
-    movq    %rax, environ(%rip)
-    movq    %rax,operatingsystem_parameter_envp(%rip)
-    movq    -32(%rbp), %rax
-    movq    (%rax), %rax
-    movq    %rax, -8(%rbp)
-    cmpq    $0, -8(%rbp)
-    je      .L2
-    movq    -8(%rbp), %rdi
-    movl    $47, %esi
-    call    _strrchr
-    movq    %rax, __progname(%rip)
-    movq    __progname(%rip), %rax
-    testq   %rax, %rax
-    jne     .L4
-    movq    -8(%rbp), %rax
-    movq    %rax, __progname(%rip)
-    jmp     .L6
-.L4:
-    movq    __progname(%rip), %rax
-    addq    $1, %rax
-    movq    %rax, __progname(%rip)
-.L6:
-    leaq    __progname_storage(%rip), %rax
-    movq    %rax, -16(%rbp)
-    jmp     .L7
-.L8:
-    movq    __progname(%rip), %rcx
-    movzbl  (%rcx), %edx
-    movq    -16(%rbp), %rax
-    movb    %dl, (%rax)
-    addq    $1, -16(%rbp)
-    leaq    1(%rcx), %rax
-    movq    %rax, __progname(%rip)
-.L7:
-    movq    __progname(%rip), %rax
-    movzbl  (%rax), %eax
-    testb   %al, %al
-    je      .L9
-    leaq    __progname_storage+255(%rip), %rax
-    cmpq    %rax, -16(%rbp)
-    jb      .L8
-.L9:
-    leaq    __progname_storage(%rip), %rax
-    movq    %rax, __progname(%rip)
-    movq    -16(%rbp), %rax
-    movb    $0, (%rax)
-.L2:
-    movq    _mcleanup@GOTPCREL(%rip), %rdi
-    call    atexit
-    movq    _etext@GOTPCREL(%rip), %rsi
-    movq    _eprol(%rip), %rdi
-    call    monstartup@plt
-    movl    $0, %eax
-    call    _init
-    movq    environ(%rip), %rdx
-    movq    -32(%rbp), %rsi
-    movl    -20(%rbp), %edi
-    movq    %rdi,operatingsystem_parameter_argc(%rip)
-    movq    %rsi,operatingsystem_parameter_argv(%rip)
-    movl    $0, %eax
-    call    PASCALMAIN
-    // movl    %eax, %edi
-    // call    exit
-    jmp     _FPC_proc_haltproc
+procedure _FPC_proc___start(argc: LongInt; argv: PPChar; envp: Pointer; para1, para2, para3: QWord);
+  var
+    I: SizeUInt;
+  begin
+    environ:=envp;
+    operatingsystem_parameter_envp:=envp;
+    operatingsystem_parameter_argc:=argc;
+    operatingsystem_parameter_argv:=argv;
+    if argv[0]<>nil then
+      begin
+        __progname:=_strrchr(argv[0], Ord('/'));
+        if __progname<>nil then
+          Inc(__progname)
+        else
+          __progname:=argv[0];
+        I:=Low(__progname_storage);
+        while (I<High(__progname_storage)) and (__progname[I]<>#0) do
+          begin
+            __progname_storage[I]:=__progname[I-Low(__progname_storage)];
+            Inc(I);
+          end;
+        __progname_storage[I]:=#0;
+        __progname:=@__progname_storage;
+      end;
+{    atexit(@_mcleanup);
+    monstartup(u_long(@_eprol),u_long(@_etext));}
+    __init;
+    PascalMain;
+    c_exit(operatingsystem_result);
   end;
 
-procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
-  asm
-    movq    $1,%rax
-    movl    operatingsystem_result(%rip),%ebx
-    pushq   %rbx
-    call    .Lactualsyscall
-    addq    $8,%rsp
-    jmp     _FPC_proc_haltproc
-
-.Lactualsyscall:
-    int     $0x80
-    jb      .LErrorcode
-    xor     %rbx,%rbx
-    ret
-.LErrorcode:
-    movq    %rax,%rbx
-    movq    $-1,%rax
+procedure _FPC_proc_haltproc; noreturn; public name '_haltproc';
+  begin
+    c_exit(operatingsystem_result);
   end;
 
-function _strrchr(str: PChar; character: LongInt): PChar; assembler; nostackframe; public name '_strrchr';
-  asm
-    pushq   %rbp
-    movq    %rsp, %rbp
-    movq    %rdi, -24(%rbp)
-    movb    %sil, -25(%rbp)
-    movq    $0, -8(%rbp)
-.L13:
-    movq    -24(%rbp), %rdx
-    movzbl  (%rdx), %eax
-    cmpb    -25(%rbp), %al
-    jne     .L14
-    movq    -24(%rbp), %rax
-    movq    %rax, -8(%rbp)
-.L14:
-    movq    -24(%rbp), %rdx
-    movzbl  (%rdx), %eax
-    testb   %al, %al
-    jne     .L16
-    movq    -8(%rbp), %rax
-    movq    %rax, -16(%rbp)
-    jmp     .L12
-.L16:
-    addq    $1, -24(%rbp)
-    jmp     .L13
-.L12:
-    movq    -16(%rbp), %rax
-    leave
+function _strrchr(str: PChar; character: LongInt): PChar; public name '_strrchr';
+  begin
+    _strrchr:=nil;
+    repeat
+      if str^=Chr(character) then
+        _strrchr:=str;
+      if str^<>#0 then
+        Inc(str);
+    until str^=#0;
   end;
 
-procedure MD_EPROL_LABEL; assembler; nostackframe; public name '_eprol';
+{procedure MD_EPROL_LABEL; assembler; nostackframe; public name '_eprol';
   asm
-  end;
+  end;}

+ 15 - 143
rtl/openbsd/x86_64/si_dll.inc

@@ -16,119 +16,22 @@
 
 {$asmmode gas}
 
+{$ifdef VER3_0}
 procedure _init; external name '_init';
-
-procedure _FPC_proc___start; forward;
-
-procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public name '__start';
-  asm
-    movq    %rbx,%r9
-    movq    %rcx,%r8
-    movq    %rdx,%rcx
-    movq    (%rsp),%rdi
-    leaq    16(%rsp,%rdi,8),%rdx
-    leaq    8(%rsp),%rsi
-    subq    $8,%rsp
-    andq    $0xFFFFFFFFFFFFFFF0,%rsp
-    addq    $8,%rsp
-    jmp     _FPC_proc___start
-  end;
-
-procedure _FPC_proc_haltproc; forward;
-function _strrchr(str: PChar; character: LongInt): PChar; forward;
-
-procedure _FPC_proc___start; assembler; nostackframe; public name '___start';
-  asm
-    pushq   %rbp
-    movq    %rsp, %rbp
-    subq    $64, %rsp
-    movl    %edi, -20(%rbp)
-    movq    %rsi, -32(%rbp)
-    movq    %rdx, -40(%rbp)
-    movq    %rcx, -48(%rbp)
-    movq    %r8, -56(%rbp)
-    movq    %r9, -64(%rbp)
-    movq    environ@GOTPCREL(%rip), %rdx
-    movq    -40(%rbp), %rax
-    movq    %rax, (%rdx)
-    movq    operatingsystem_parameter_envp@GOTPCREL(%rip), %rdx
-    movq    -40(%rbp), %rax
-    movq    %rax, (%rdx)
-
-    movl    -20(%rbp), %eax
-    movslq  %eax,%rdx
-    movq    operatingsystem_parameter_argc@GOTPCREL(%rip), %rax
-    movq    %rdx, (%rax)
-    movq    operatingsystem_parameter_argv@GOTPCREL(%rip), %rdx
-    movq    -32(%rbp), %rax
-    movq    %rax, (%rdx)
-    movq    -32(%rbp), %rax
-    movq    (%rax), %rax
-    movq    %rax, -8(%rbp)
-    cmpq    $0, -8(%rbp)
-    je      .L2
-    movq    -8(%rbp), %rdi
-    movl    $47, %esi
-    call    _strrchr
-    movq    %rax, %rdx
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    %rdx, (%rax)
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    (%rax), %rax
-    testq   %rax, %rax
-    jne     .L4
-    movq    __progname@GOTPCREL(%rip), %rdx
-    movq    -8(%rbp), %rax
-    movq    %rax, (%rdx)
-    jmp     .L6
-.L4:
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    (%rax), %rax
-    leaq    1(%rax), %rdx
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    %rdx, (%rax)
-.L6:
-    movq    __progname_storage@GOTPCREL(%rip), %rax
-    movq    %rax, -16(%rbp)
-    jmp     .L7
-.L8:
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    (%rax), %rcx
-    movzbl  (%rcx), %edx
-    movq    -16(%rbp), %rax
-    movb    %dl, (%rax)
-    addq    $1, -16(%rbp)
-    leaq    1(%rcx), %rdx
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    %rdx, (%rax)
-.L7:
-    movq    __progname@GOTPCREL(%rip), %rax
-    movq    (%rax), %rax
-    movzbl  (%rax), %eax
-    testb   %al, %al
-    je      .L9
-    movq    __progname_storage@GOTPCREL(%rip), %rax
-    leaq    255(%rax), %rax
-    cmpq    %rax, -16(%rbp)
-    jb      .L8
-.L9:
-    movq    -16(%rbp), %rax
-    movb    $0, (%rax)
-    movq    __progname@GOTPCREL(%rip), %rdx
-    movq    __progname_storage@GOTPCREL(%rip), %rax
-    movq    %rax, (%rdx)
-.L2:
-    movl    $0, %eax
-    call    _init@PLT
-    movq    environ@GOTPCREL(%rip), %rax
-    movq    (%rax), %rdx
-    movq    -32(%rbp), %rsi
-    movl    -20(%rbp), %edi
-    movl    $0, %eax
-    call    PASCALMAIN@PLT
-    // movl    %eax, %edi
-    // call    exit
-    jmp     _FPC_proc_haltproc@PLT
+{$else VER3_0}
+procedure _init; weakexternal name '_init';
+{$endif VER3_0}
+
+procedure _FPC_shared_lib_start; cdecl; public name 'FPC_LIB_START';
+  begin
+    { todo: figure out if there's any way to obtain these in OpenBSD shared libraries }
+    environ:=nil;
+    operatingsystem_parameter_envp:=nil;
+    operatingsystem_parameter_argc:=0;
+    operatingsystem_parameter_argv:=nil;
+    if Assigned(@_init) then
+      _init;
+    PascalMain;
   end;
 
 procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
@@ -149,34 +52,3 @@ procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
     movq    %rax,%rbx
     movq    $-1,%rax
   end;
-
-function _strrchr(str: PChar; character: LongInt): PChar; assembler; nostackframe; public name '_strrchr';
-  asm
-    pushq   %rbp
-    movq    %rsp, %rbp
-    movq    %rdi, -24(%rbp)
-    movb    %sil, -25(%rbp)
-    movq    $0, -8(%rbp)
-.L13:
-    movq    -24(%rbp), %rdx
-    movzbl  (%rdx), %eax
-    cmpb    -25(%rbp), %al
-    jne     .L14
-    movq    -24(%rbp), %rax
-    movq    %rax, -8(%rbp)
-.L14:
-    movq    -24(%rbp), %rdx
-    movzbl  (%rdx), %eax
-    testb   %al, %al
-    jne     .L16
-    movq    -8(%rbp), %rax
-    movq    %rax, -16(%rbp)
-    jmp     .L12
-.L16:
-    addq    $1, -24(%rbp)
-    jmp     .L13
-.L12:
-    movq    -16(%rbp), %rax
-    leave
-  end;
-

+ 97 - 0
rtl/openbsd/x86_64/si_g.inc

@@ -0,0 +1,97 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2019 by Free Pascal development team
+
+    This file implements parts of the startup code for OpenBSD
+    programs, compiled with gprof support.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+{$asmmode gas}
+
+var
+  _etext: Byte; external name '_etext';
+  _eprol: Byte; external name '_eprol';
+
+procedure _mcleanup; cdecl; external name '_mcleanup';
+function atexit(proc: TCdeclProcedure): cint; cdecl; external name 'atexit';
+procedure _monstartup(lowpc, highpc: u_long); cdecl; external name '_monstartup';
+procedure __init; cdecl; external name '__init';
+procedure c_exit(exit_code: cint); cdecl; noreturn; external name 'exit';
+
+procedure _FPC_proc___start(argc: LongInt; argv: PPChar; envp: Pointer; para1, para2, para3: QWord); forward;
+
+procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public name '__start';
+  asm
+    movq    %rbx,%r9
+    movq    %rcx,%r8
+    movq    %rdx,%rcx
+    movq    (%rsp),%rdi
+    leaq    16(%rsp,%rdi,8),%rdx
+    leaq    8(%rsp),%rsi
+    subq    $8,%rsp
+    andq    $0xFFFFFFFFFFFFFFF0,%rsp
+    addq    $8,%rsp
+    jmp     _FPC_proc___start
+  end;
+
+procedure _FPC_proc_haltproc; noreturn; forward;
+function _strrchr(str: PChar; character: LongInt): PChar; forward;
+
+procedure _FPC_proc___start(argc: LongInt; argv: PPChar; envp: Pointer; para1, para2, para3: QWord);
+  var
+    I: SizeUInt;
+  begin
+    environ:=envp;
+    operatingsystem_parameter_envp:=envp;
+    operatingsystem_parameter_argc:=argc;
+    operatingsystem_parameter_argv:=argv;
+    if argv[0]<>nil then
+      begin
+        __progname:=_strrchr(argv[0], Ord('/'));
+        if __progname<>nil then
+          Inc(__progname)
+        else
+          __progname:=argv[0];
+        I:=Low(__progname_storage);
+        while (I<High(__progname_storage)) and (__progname[I]<>#0) do
+          begin
+            __progname_storage[I]:=__progname[I-Low(__progname_storage)];
+            Inc(I);
+          end;
+        __progname_storage[I]:=#0;
+        __progname:=@__progname_storage;
+      end;
+    atexit(@_mcleanup);
+    _monstartup(u_long(@_eprol),u_long(@_etext));
+    __init;
+    PascalMain;
+    c_exit(operatingsystem_result);
+  end;
+
+procedure _FPC_proc_haltproc; noreturn; public name '_haltproc';
+  begin
+    c_exit(operatingsystem_result);
+  end;
+
+function _strrchr(str: PChar; character: LongInt): PChar; public name '_strrchr';
+  begin
+    _strrchr:=nil;
+    repeat
+      if str^=Chr(character) then
+        _strrchr:=str;
+      if str^<>#0 then
+        Inc(str);
+    until str^=#0;
+  end;
+
+procedure MD_EPROL_LABEL; assembler; nostackframe; public name '_eprol';
+  asm
+  end;

+ 38 - 107
rtl/openbsd/x86_64/si_prc.inc

@@ -16,7 +16,7 @@
 
 {$asmmode gas}
 
-procedure _FPC_proc___start; forward;
+procedure _FPC_proc___start(argc: LongInt; argv: PPChar; envp: Pointer; para1, para2, para3: QWord); forward;
 
 procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public name '__start';
   asm
@@ -35,84 +35,34 @@ procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public
 procedure _FPC_proc_haltproc; forward;
 function _strrchr(str: PChar; character: LongInt): PChar; forward;
 
-procedure _FPC_proc___start; assembler; nostackframe; public name '___start';
-  asm
-    pushq   %rbp
-    movq    %rsp, %rbp
-    subq    $64, %rsp
-    movl    %edi, -20(%rbp)
-    movq    %rsi, -32(%rbp)
-    movq    %rdx, -40(%rbp)
-    movq    %rcx, -48(%rbp)
-    movq    %r8, -56(%rbp)
-    movq    %r9, -64(%rbp)
-    movq    -40(%rbp), %rax
-    movq    %rax, environ(%rip)
-    movq    %rax,operatingsystem_parameter_envp(%rip)
-    movq    -32(%rbp), %rax
-    movq    (%rax), %rax
-    movq    %rax, -8(%rbp)
-    cmpq    $0, -8(%rbp)
-    je      .L2
-    movq    -8(%rbp), %rdi
-    movl    $47, %esi
-    call    _strrchr
-    movq    %rax, __progname(%rip)
-    movq    __progname(%rip), %rax
-    testq   %rax, %rax
-    jne     .L4
-    movq    -8(%rbp), %rax
-    movq    %rax, __progname(%rip)
-    jmp     .L6
-.L4:
-    movq    __progname(%rip), %rax
-    addq    $1, %rax
-    movq    %rax, __progname(%rip)
-.L6:
-    leaq    __progname_storage(%rip), %rax
-    movq    %rax, -16(%rbp)
-    jmp     .L7
-.L8:
-    movq    __progname(%rip), %rcx
-    movzbl  (%rcx), %edx
-    movq    -16(%rbp), %rax
-    movb    %dl, (%rax)
-    addq    $1, -16(%rbp)
-    leaq    1(%rcx), %rax
-    movq    %rax, __progname(%rip)
-.L7:
-    movq    __progname(%rip), %rax
-    movzbl  (%rax), %eax
-    testb   %al, %al
-    je      .L9
-    leaq    __progname_storage+255(%rip), %rax
-    cmpq    %rax, -16(%rbp)
-    jb      .L8
-.L9:
-    leaq    __progname_storage(%rip), %rax
-    movq    %rax, __progname(%rip)
-    movq    -16(%rbp), %rax
-    movb    $0, (%rax)
-.L2:
-    // movl    $_mcleanup, %edi
-    // call    atexit
-    // movl    $_etext, %eax
-    // movq    %rax, %rsi
-    // movl    $_eprol, %eax
-    // movq    %rax, %rdi
-    // call    monstartup
-    // movl    $0, %eax
-    // call    __init
-    movq    environ(%rip), %rdx
-    movq    -32(%rbp), %rsi
-    movl    -20(%rbp), %edi
-    movq    %rdi,operatingsystem_parameter_argc(%rip)
-    movq    %rsi,operatingsystem_parameter_argv(%rip)
-    movl    $0, %eax
-    call    PASCALMAIN
-    // movl    %eax, %edi
-    // call    exit
-    jmp     _FPC_proc_haltproc
+procedure _FPC_proc___start(argc: LongInt; argv: PPChar; envp: Pointer; para1, para2, para3: QWord);
+  var
+    I: SizeUInt;
+  begin
+    environ:=envp;
+    operatingsystem_parameter_envp:=envp;
+    operatingsystem_parameter_argc:=argc;
+    operatingsystem_parameter_argv:=argv;
+    if argv[0]<>nil then
+      begin
+        __progname:=_strrchr(argv[0], Ord('/'));
+        if __progname<>nil then
+          Inc(__progname)
+        else
+          __progname:=argv[0];
+        I:=Low(__progname_storage);
+        while (I<High(__progname_storage)) and (__progname[I]<>#0) do
+          begin
+            __progname_storage[I]:=__progname[I-Low(__progname_storage)];
+            Inc(I);
+          end;
+        __progname_storage[I]:=#0;
+        __progname:=@__progname_storage;
+      end;
+    PascalMain;
+    asm
+      jmp     _FPC_proc_haltproc
+    end;
   end;
 
 procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
@@ -134,32 +84,13 @@ procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
     movq    $-1,%rax
   end;
 
-function _strrchr(str: PChar; character: LongInt): PChar; assembler; nostackframe; public name '_strrchr';
-  asm
-    pushq   %rbp
-    movq    %rsp, %rbp
-    movq    %rdi, -24(%rbp)
-    movb    %sil, -25(%rbp)
-    movq    $0, -8(%rbp)
-.L13:
-    movq    -24(%rbp), %rdx
-    movzbl  (%rdx), %eax
-    cmpb    -25(%rbp), %al
-    jne     .L14
-    movq    -24(%rbp), %rax
-    movq    %rax, -8(%rbp)
-.L14:
-    movq    -24(%rbp), %rdx
-    movzbl  (%rdx), %eax
-    testb   %al, %al
-    jne     .L16
-    movq    -8(%rbp), %rax
-    movq    %rax, -16(%rbp)
-    jmp     .L12
-.L16:
-    addq    $1, -24(%rbp)
-    jmp     .L13
-.L12:
-    movq    -16(%rbp), %rax
-    leave
+function _strrchr(str: PChar; character: LongInt): PChar; public name '_strrchr';
+  begin
+    _strrchr:=nil;
+    repeat
+      if str^=Chr(character) then
+        _strrchr:=str;
+      if str^<>#0 then
+        Inc(str);
+    until str^=#0;
   end;