ignore_init.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. {
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright 2012 Konstantin Belousov <[email protected]>
  5. * Copyright (c) 2018 The FreeBSD Foundation
  6. *
  7. * Parts of this software was developed by Konstantin Belousov
  8. * <[email protected]> under sponsorship from the FreeBSD Foundation.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. Translated into Pascal by Marco van de Voort
  30. }
  31. //#include <sys/cdefs.h>
  32. //__FBSDID("$FreeBSD: releng/12.1/lib/csu/common/ignore_init.c 339351 2018-10-13 23:52:55Z kib $");
  33. //#include <sys/param.h>
  34. //#include <sys/elf.h>
  35. //#include <sys/elf_common.h>
  36. //#include "notes.h"
  37. // replaced by pascalmain in si_c.pp
  38. //function cmain(argc:longint;argv:PPAnsiChar;env:PPAnsiChar):longint; cdecl; external name '_main';
  39. Type TInitProc = procedure(argc:longint;argv:PPAnsiChar;env:PPAnsiChar); cdecl;
  40. PInitProc = ^TInitProc;
  41. TCleanupProc = procedure; cdecl;
  42. PCleanupProc = ^TCleanupProc;
  43. procedure atexit(prc:TCleanupProc); cdecl external name 'atexit';
  44. procedure cleanup(prc:TCleanupProc); cdecl external name 'cleanup';
  45. var
  46. environ : PPAnsiChar; cvar; public name '__environ';
  47. _Dynamic : longint; weakexternal name '_DYNAMIC';
  48. var
  49. preinit_array_start : PInitProc; external name '__preinit_array_start';
  50. preinit_array_end : PInitProc; external name '__preinit_array_end';
  51. init_array_start : PInitProc; external name '__init_array_start';
  52. init_array_end : PInitProc; external name '__init_array_end';
  53. fini_array_start : PCleanupProc; external name '__fini_array_start';
  54. fini_array_end : PCleanupProc; external name '__fini_array_end';
  55. procedure _fini; cdecl; external name '_fini';
  56. procedure _init; cdecl; external name '_init';
  57. procedure libc_exit(exitcode:longint);cdecl; external name 'exit';
  58. Type
  59. // I only doublechecked these for don't know how these records are defined, but they are resp. 24 and 16 byte on 64-bit
  60. Elf_Rela = record
  61. r_offset : uint64 {Elf64_Addr}; { Location to be relocated. }
  62. r_info : uint64 {Elf64_Xword}; { Relocation type and symbol index. }
  63. r_addend : uint64 {Elf64_Sxword}; { Addend. }
  64. end;
  65. PElf_Rela = ^Elf_Rela;
  66. Elf_Rel = record
  67. r_offset : uint64 {Elf64_Addr}; { Location to be relocated. }
  68. r_info : uint64 {Elf64_Xword}; { Relocation type and symbol index. }
  69. end;
  70. PElf_Rel = ^Elf_Rel;
  71. {$if defined(CRT_IRELOC_RELA)}
  72. var
  73. rela_iplt_start : Elf_rela; weakexternal name '__rela_iplt_start';
  74. rela_iplt_end : Elf_rela; weakexternal name '__rela_iplt_end';
  75. {$include reloc.inc}
  76. procedure process_irelocs; cdecl;
  77. var p,p2 : pElf_Rela;
  78. begin
  79. p:=@rela_iplt_start;
  80. p2:=@rela_iplt_end;
  81. while (p<p2) do
  82. begin
  83. crt1_handle_rela(p);
  84. inc(p);
  85. end;
  86. end;
  87. {$elseif defined(CRT_IRELOC_REL)}
  88. var
  89. rel_iplt_start : Elf_Rel; weakexternal name '__rel_iplt_start';
  90. rel_iplt_end : Elf_Rel; weakexternal name '__rel_iplt_end';
  91. {$include reloc.inc}
  92. procedure process_irelocs; cdecl;
  93. var p,p2 : pElf_Rel;
  94. begin
  95. p:=@rel_iplt_start;
  96. p2:=@rel_iplt_end;
  97. while (p<p2) do
  98. begin
  99. crt1_handle_rel(p)
  100. inc(p);
  101. end;
  102. end;
  103. {$elseif defined(CRT_IRELOC_SUPPRESS)}
  104. {$else}
  105. {$error 'Define platform reloc type'}
  106. {$endif}
  107. procedure finalizer; cdecl;
  108. var
  109. fn : TCleanupProc;
  110. n,array_size : ptruint; // actually: size_t;
  111. begin
  112. array_size:= fini_array_end - fini_array_start;
  113. n:=array_size;
  114. while n>0 do
  115. begin
  116. fn := fini_array_start[n - 1];
  117. if assigned(fn) and (ptrint(fn)<>-1) then
  118. fn();
  119. dec(n);
  120. end;
  121. _fini();
  122. end;
  123. procedure handle_static_init(argc:longint;argv:PPAnsiChar;env:PPAnsiChar); cdecl;
  124. var fn : TInitProc;
  125. n,array_size : ptruint; // actually: size_t;
  126. begin
  127. if assigned(@_dynamic) then
  128. exit;
  129. atexit(@finalizer);
  130. array_size := preinit_array_end - preinit_array_start;
  131. n:=0;
  132. while n<array_size do
  133. begin
  134. fn := preinit_array_start[n];
  135. if assigned(fn) and (ptrint(fn)<>-1) then
  136. fn(argc,argv,env);
  137. inc(n);
  138. end;
  139. _init();
  140. n:=0;
  141. while n<array_size do
  142. begin
  143. fn := init_array_start[n];
  144. if assigned(fn) and (ptrint(fn)<>-1) then
  145. fn(argc,argv,env);
  146. inc(n);
  147. end;
  148. end;
  149. procedure handle_argv(argc:longint;argv:PPAnsiChar;env:PPAnsiChar); inline;
  150. var
  151. s: PAnsiChar;
  152. begin
  153. if assigned(environ) then
  154. environ:=env;
  155. if (argc>0) and assigned(argv[0]) then
  156. begin
  157. progname:=argv[0];
  158. s:=progname;
  159. while s^<>#0 do
  160. begin
  161. if s^='/' then
  162. progname:=@s[1];
  163. inc(s);
  164. end;
  165. end;
  166. end;
  167. (*
  168. static const struct {
  169. int32_t namesz;
  170. int32_t descsz;
  171. int32_t type;
  172. AnsiChar name[sizeof(NOTE_FREEBSD_VENDOR)];
  173. uint32_t desc;
  174. } crt_noinit_tag __attribute__ ((section (NOTE_SECTION),
  175. aligned(4))) __used = {
  176. .namesz = sizeof(NOTE_FREEBSD_VENDOR),
  177. .descsz = sizeof(uint32_t),
  178. .type = NT_FREEBSD_NOINIT_TAG,
  179. .name = NOTE_FREEBSD_VENDOR,
  180. .desc = 0
  181. };
  182. *)