Advertisement
DarthGizka

BC++ 5.02 c0nt.asm

Dec 28th, 2014
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; C0NT.ASM - C run time library entry point for Win32
  2.  
  3. ;
  4. ;       C/C++ Run Time Library - Version 2.0
  5. ;
  6. ;       Copyright (c) 1991, 1996 by Borland International
  7. ;       All Rights Reserved.
  8. ;
  9.  
  10.                 .386P
  11. ifndef MASM
  12.                 model flat
  13. endif
  14.                 ASSUME   CS: FLAT, DS: FLAT, SS: FLAT, ES: FLAT
  15.  
  16. BSSINIT         equ     1       ; Fix for problem with OS not initializing
  17.                                 ;  the first page of BSS to zero
  18. FULLBSS         equ     1       ; Fix for problem with OS not initializing
  19.                                 ;  the entire BSS to zero
  20.  
  21. ;----------------------------------------------------------------------
  22. ; Code segment
  23.  
  24. _TEXT           SEGMENT PUBLIC DWORD USE32 PUBLIC 'CODE'
  25. _TEXT           ENDS
  26.  
  27. ;----------------------------------------------------------------------
  28. ; Data segments
  29.  
  30. _DATA           SEGMENT PUBLIC DWORD USE32 PUBLIC 'DATA'
  31.                 db      'Borland C++ - Copyright 1995 Borland Intl.',0
  32. _DATA           ENDS
  33.  
  34. _TLSCBA         SEGMENT PUBLIC DWORD USE32 PUBLIC 'TLSCBA'
  35. _TLSCBA         ENDS
  36.  
  37. _INIT_          SEGMENT PUBLIC WORD USE32 PUBLIC 'INITDATA'
  38. InitStart       label byte
  39. _INIT_          ENDS
  40.  
  41. _INITEND_       SEGMENT PUBLIC WORD USE32 PUBLIC 'INITDATA'
  42. InitEnd         label byte
  43. _INITEND_       ENDS
  44.  
  45. _EXIT_          SEGMENT PUBLIC WORD USE32 PUBLIC 'EXITDATA'
  46. ExitStart       label byte
  47. _EXIT_          ENDS
  48.  
  49. _EXITEND_       SEGMENT PUBLIC WORD USE32 PUBLIC 'EXITDATA'
  50. ExitEnd         label byte
  51. _EXITEND_       ENDS
  52.  
  53. ifndef MASM
  54.                 NOWARN  RES
  55. endif
  56. CONST           SEGMENT PUBLIC DWORD USE32 PUBLIC 'CONST'
  57. CONST           ENDS
  58. ifndef MASM
  59.                 WARN    RES
  60. endif
  61.  
  62. _BSS            SEGMENT PUBLIC DWORD USE32 PUBLIC 'BSS'
  63. IFDEF   BSSINIT
  64. bdata@          label   byte
  65. ENDIF
  66. _BSS            ENDS
  67.  
  68. IFDEF   BSSINIT
  69. _BSSEND         SEGMENT PUBLIC DWORD USE32 PUBLIC 'BSS'
  70. edata@          label   byte
  71. _BSSEND         ENDS
  72. ENDIF
  73.  
  74. IFDEF   BSSINIT
  75. DGROUP          GROUP _DATA, _TLSCBA, _INIT_, _INITEND_, _EXIT_, _EXITEND_, CONST, _BSS, _BSSEND
  76. ELSE
  77. DGROUP          GROUP _DATA, _TLSCBA, _INIT_, _INITEND_, _EXIT_, _EXITEND_, CONST, _BSS
  78. ENDIF
  79.  
  80. ;----------------------------------------------------------------------
  81. ; External function definitions
  82.  
  83. _TEXT           SEGMENT PUBLIC DWORD USE32 PUBLIC 'CODE'
  84.  
  85. ifdef DLL       ; DLL startup
  86.  
  87. EXTRN           __startupd:NEAR
  88. EXTRN           __ErrorMessage:NEAR
  89. EXTRN       __free_heaps:NEAR
  90. EXTRN           DllEntryPoint:NEAR
  91. EXTRN           GetVersion:NEAR
  92.  
  93. else            ; EXE startup
  94.  
  95. ifdef WINDOWS
  96. EXTRN           WinMain:NEAR
  97. else
  98. EXTRN           _main:NEAR
  99. endif
  100.  
  101. endif
  102.  
  103. EXTRN           __startup:NEAR
  104. EXTRN           __matherr:NEAR
  105. EXTRN           __matherrl:NEAR
  106.  
  107. ifndef  NOXX
  108. EXTRN           __ExceptInit:NEAR
  109. EXTRN           __GetExceptDLLinfoInternal:NEAR
  110. EXTRN           __DestructorCountPtr:DWORD
  111. endif
  112.  
  113. EXTRN           GetModuleHandleA:NEAR
  114.  
  115. _TEXT           ENDS
  116.  
  117. ;----------------------------------------------------------------------
  118. ; External data definitions
  119.  
  120. _DATA           SEGMENT PUBLIC DWORD USE32 'DATA'
  121.  
  122. ifndef DLL       ; EXE startup
  123. EXTRN           __fmode:NEAR
  124. endif
  125.  
  126.  
  127. _DATA           ENDS
  128.  
  129. ;----------------------------------------------------------------------
  130. ; CV4 debugging information: a pointer to main, WinMain, or DllEntryPoint
  131.  
  132. ifndef MASM ; MASM386 can't handle 'dp' directive
  133.  
  134. $$BSYMS         SEGMENT PUBLIC BYTE USE32 'DEBSYM'
  135.                 dd  2       ; magic
  136.                 dw  8       ; magic
  137.                 dw  210h    ; magic (S_ENTRY32)
  138. ifdef DLL
  139.                 dp  DllEntryPoint
  140. else
  141. ifdef WINDOWS
  142.                 dp  WinMain
  143. else
  144.                 dp  _main
  145. endif
  146. endif
  147. $$BSYMS         ENDS
  148.  
  149. endif   ; ndef MASM
  150.  
  151. ;----------------------------------------------------------------------
  152. ; MODULE_DATA table.
  153. ;
  154. ; This table contains information about this EXE or DLL that is used
  155. ; by startup.c to properly initialize or clean up the module.
  156. ; IMPORTANT: the layout must exactly match the MODULE_DATA structure
  157. ; definition in startup.c!
  158.  
  159.  
  160. _DATA           SEGMENT PUBLIC DWORD USE32 'DATA'
  161.  
  162.                 align   4
  163. module_data     dd      offset FLAT:InitStart
  164.                 dd      offset FLAT:InitEnd
  165.                 dd      offset FLAT:ExitStart
  166.                 dd      offset FLAT:ExitEnd
  167. ifdef WINDOWS
  168.                 dd      1                       ; flags
  169. else
  170.                 dd      0                       ; flags
  171. endif
  172.                 dd      0                       ; module handle
  173. ifdef DLL
  174.                 dd      offset FLAT:DllEntryPoint
  175.                 dd      0                       ; matherr
  176.                 dd      0                       ; matherrl
  177.                 dd      0                       ; stack base
  178.                 dd      0                       ; fmode
  179. else
  180. ifdef WINDOWS
  181.                 dd      offset FLAT:WinMain
  182. else
  183.                 dd      offset FLAT:_main
  184. endif
  185.                 dd      offset FLAT:__matherr
  186.                 dd      offset FLAT:__matherrl
  187.                 dd      0                       ; stack base
  188.                 dd      offset FLAT:__fmode
  189. endif
  190.  
  191. public          ___isDLL
  192. ifdef DLL
  193. ___isDLL        db      1
  194. else
  195. ___isDLL        db      0
  196. endif
  197.  
  198. public          ___isGUI
  199. ifdef DLL
  200. ___isGUI        db      1
  201. else
  202. ifdef WINDOWS
  203. ___isGUI        db      1
  204. else
  205. ___isGUI        db      0
  206. endif
  207. endif
  208.  
  209. public          __TLS_index
  210. __TLS_index     dd      0
  211. public          __TLS_index4
  212. __TLS_index4    dd      0
  213.  
  214. public          __hInstance
  215. __hInstance     dd      0  ; Global _hInstance
  216.  
  217. saveStackTop    dd      0,0
  218. saveStackBase   dd      0
  219.  
  220. ifdef DLL
  221. usecount        db      0
  222. nonsharedMsg    db      'Nonshared DATA segment required',0
  223. win32sMsg       db      'Cannot run multiple instances of a DLL under WIN32s',0
  224. endif
  225. _data           ENDS
  226.  
  227. ;----------------------------------------------------------------------
  228. ; Startup code
  229.  
  230. _TEXT           SEGMENT  DWORD USE32 PUBLIC 'CODE'
  231.  
  232.                 public __acrtused
  233. __acrtused      PROC NEAR
  234.  
  235.                 mov     eax, __TLS_index
  236.                 shl     eax, 2
  237.                 mov     __TLS_index4, eax
  238. ifdef DLL
  239. ;;  If we are attaching to a process, make sure that we have a per-instance data segment.
  240. ;;  If we do not, then determine whether it's due to NONSHARED or Win32s limitation.
  241.                 cmp     dword ptr [esp+8], 1
  242.                 jne     not_process_attach
  243.                 cmp     usecount, 0
  244.                 je      firstInit
  245.                 call    GetVersion
  246.                 mov     edx, offset nonsharedMsg
  247.                 and     eax, 080000000h
  248.                 jz      msgSet
  249.                 mov     edx, offset win32sMsg
  250. msgSet:
  251.                 push    edx
  252.                 call    __ErrorMessage
  253.                 pop     eax
  254.                 xor     eax, eax
  255.                 jmp     return
  256. firstInit:
  257.                 inc     usecount
  258. endif ;DLL
  259.  
  260. ;----------------------------------------------------------------------
  261. ifdef   BSSINIT
  262. ifdef   DLL
  263.                 cmp     dword ptr [esp+8], 1    ; Check whether Process Attach
  264.                 jne     skipBSSInit
  265. endif
  266.                 push    edi
  267.                 push    ecx
  268.  
  269.                 xor     eax, eax
  270.                 mov     edi, offset DGROUP: bdata@
  271.                 mov     ecx, offset DGROUP: edata@
  272.                 cmp     ecx, edi
  273.                 jna     BSSInitDone
  274. ifdef   FULLBSS
  275.                 sub     ecx, edi
  276. else
  277.                 mov     ecx, edi
  278.                 and     ecx, 0fffh
  279.                 jecxz   BSSInitDone
  280.                 sub     ecx, 1000h
  281.                 neg     ecx
  282. endif   ;FULLBSS
  283.                 cld
  284.                 rep     stosb
  285. BSSInitDone:
  286.                 pop     ecx
  287.                 pop     edi
  288. skipBSSInit:
  289. endif   ;BSSINIT
  290. ;----------------------------------------------------------------------
  291.  
  292. ifndef  NOXX
  293. ;;  Save off stack top and stack base so we can restore them later and
  294. ;;  to check and make sure we are on the correct stack during shutdown
  295.                 mov     edx, fs:[4]
  296.                 mov     [saveStackBase], edx
  297.                 mov     eax, [edx-8]
  298.                 mov     [saveStackTop  ], eax
  299.                 mov     eax, [edx-4]
  300.                 mov     [saveStackTop+4], eax
  301.                 sub     edx, 4
  302.                 mov     [__DestructorCountPtr], edx
  303.  
  304. ifndef DLL
  305. ;;  Make sure there is room at the base of the stack
  306.                 sub     edx, 4
  307.                 cmp     edx, esp
  308.                 jae     stacktop_OK
  309.                 mov     esp, edx
  310. stacktop_OK:
  311.  
  312. endif
  313.  
  314. ;;
  315. ;;  When an EXE is loaded, all of the statically linked DLL's are
  316. ;;  initialized before the EXE starts running. That means that we
  317. ;;  have to make sure to initialize the exception-handling values
  318. ;;  before we let the DLL initialize itself, since all DLL's use
  319. ;;  the EXE's exception-handling variables; for this purpose, we
  320. ;;  call the function _ExceptInit() with a 0 argument whenever a
  321. ;;  DLL is told that it is being attached to a process. Inside
  322. ;;  this routine we will call into the EXE to get the addresses
  323. ;;  of the exception-handling variables. We also call the same
  324. ;;  function when the EXE itself is initialized.
  325.  
  326.                 push    0
  327.                 call    __ExceptInit
  328.                 pop     ecx
  329. endif   ;NOXX
  330.  
  331. not_process_attach:
  332.  
  333. ifdef DLL
  334.                 cmp     dword ptr [esp+8], 0
  335.                 jne     not_process_detach
  336.  
  337. ifndef  NOXX
  338. ;;  PROCESS_DETACH
  339.                 mov     edx, fs:[4]
  340.                 cmp     edx, [saveStackBase]
  341.                 je      not_process_detach      ; stack is fine proceed as usual
  342.  
  343. ;;  Ouch!  We aren't on the same stack as startup
  344.                 mov     edx, fs:[4]
  345.                 mov     eax, [edx-8]
  346.                 mov     [saveStackTop  ], eax
  347.                 mov     eax, [edx-4]
  348.                 mov     [saveStackTop+4], eax
  349.                 sub     edx, 4
  350.                 mov     [__DestructorCountPtr], edx
  351.  
  352.                 push    0
  353.                 call    __ExceptInit            ; reinitialize exception pointers
  354.                 pop     ecx
  355.  
  356. endif   ;NOXX
  357.  
  358. not_process_detach:
  359. endif
  360.  
  361.                 push    offset FLAT:module_data
  362. ifdef DLL
  363.                 mov     eax, [esp+8]            ; get mod_handle to
  364.                 mov     __hInstance, eax        ; initialize _hInstance
  365.                 call    __startupd
  366.                 add     esp, 4                  ; discard module_data parameter
  367.  
  368. ifndef  NOXX
  369.                 ; check whether Process Attach or Detach (1 or 0)
  370.                 test    dword ptr [esp+8], 0FFFFFFFEh
  371.                 jnz     return
  372.  
  373.                 mov     edx, fs:[4]             ; restore TOS
  374.                 mov     ecx, [saveStackTop  ]
  375.                 mov     [edx-8], ecx
  376.                 mov     ecx, [saveStackTop+4]
  377.                 mov     [edx-4], ecx
  378. endif   ;NOXX
  379.  
  380. return:
  381.                 ; check whether Process Detach (0)
  382.         or      dword ptr [esp+8], 0
  383.         jnz     returnNT
  384.         call    __free_heaps
  385.  
  386. returnNT:
  387.                 ret     12                      ; return to NT
  388. else
  389.                 push    0                       ; NULL returns current module
  390.                 call    GetModuleHandleA
  391.                 mov     __hInstance, eax
  392.  
  393.                 push    0                       ; dummy return address
  394.                 jmp     __startup
  395. endif
  396.  
  397. __acrtused      ENDP
  398.  
  399. ifndef  NOXX
  400. ifdef   DLL
  401. public          __GetExceptDLLinfo
  402. else
  403. publicdll       __GetExceptDLLinfo
  404. endif
  405.  
  406. __GetExceptDLLinfo      proc    near
  407.                 jmp     __GetExceptDLLinfoInternal
  408. __GetExceptDLLinfo      ENDP
  409. endif   ;NOXX
  410.  
  411. _TEXT           ENDS
  412.  
  413.                 END     __acrtused
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement