Advertisement
Guest User

Elf Crash

a guest
Mar 25th, 2021
271
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.70 KB | None
  1. /*
  2.  * crtbegin.c
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Handles global constructors and destructors for the OS4 GCC build.
  7.  *
  8.  *
  9.  * Portable ISO 'C' (1994) runtime library for the Amiga computer
  10.  * Copyright (c) 2002-2015 by Olaf Barthel <obarthel (at) gmx.net>
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  *
  17.  *   - Redistributions of source code must retain the above copyright
  18.  *     notice, this list of conditions and the following disclaimer.
  19.  *
  20.  *   - Neither the name of Olaf Barthel nor the names of contributors
  21.  *     may be used to endorse or promote products derived from this
  22.  *     software without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34.  * POSSIBILITY OF SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef EXEC_TYPES_H
  38. #include <exec/types.h>
  39. #endif /* EXEC_TYPES_H */
  40. #include <proto/exec.h>
  41. #include <proto/dos.h>
  42. #include <proto/elf.h>
  43.  
  44.  
  45. #ifndef _STDLIB_HEADERS_H
  46. #include "stdlib_headers.h"
  47. #endif /* _STDLIB_HEADERS_H */
  48.  
  49. #ifndef _MACROS_H
  50. #include "macros.h"
  51. #endif /* _MACROS_H */
  52.  
  53.  
  54. /*
  55.  * Dummy constructor and destructor array. The linker script will put these at the
  56.  * very beginning of section ".ctors" and ".dtors". crtend.o contains a similar entry
  57.  * with a NULL pointer entry and is put at the end of the sections. This way, the init
  58.  * code can find the global constructor/destructor pointers.
  59.  *
  60.  * WARNING:
  61.  * This hack does not work correctly with GCC 5 and higher. The optimizer
  62.  * will see a one element array and act appropriately. The current workaround
  63.  * is to use -fno-aggressive-loop-optimizations when compiling this file.
  64.  */
  65.  
  66. /****************************************************************************/
  67. void _init(void (*__CTOR_LIST__[])(void));
  68. void _fini(void (*__DTOR_LIST__[])(void));
  69.  
  70. extern int _main(void);
  71. extern int _start(char *args, int arglen, struct ExecBase *sysbase);
  72. static BOOL open_libraries(struct ExecBase *sysbase);
  73. static void close_libraries(void);
  74.  
  75. #ifndef _STDLIB_HEADERS_H
  76. #include "stdlib_headers.h"
  77. #endif /* _STDLIB_HEADERS_H */
  78.  
  79. /****************************************************************************/
  80.  
  81. #include <libraries/elf.h>
  82. #include <proto/elf.h>
  83.  
  84. struct Library  *ElfBase;
  85. struct ElfIFace *IElf;
  86.  
  87. /****************************************************************************/
  88.  
  89. void shared_obj_init(void);
  90. void shared_obj_exit(void);
  91.  
  92. /****************************************************************************/
  93.  
  94. static void SHLibsInit(BOOL init) {
  95.     Elf32_Handle hSelf = (Elf32_Handle)NULL;
  96.     BPTR segment_list = GetProcSegList(NULL, GPSLF_RUN | GPSLF_SEG);
  97.     if (segment_list != ZERO)
  98.     {
  99.         int ret = GetSegListInfoTags(segment_list, GSLI_ElfHandle, &hSelf, TAG_DONE);
  100.         if (ret == 1)
  101.         {
  102.             if (hSelf != NULL)
  103.             {
  104.                 Printf("Below the crash\n");
  105.                 /* Trigger the constructors, etc. in the shared objects linked to this binary. */
  106.                 InitSHLibs(hSelf, init);
  107.                 Printf("No crash on static linking\n");
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113. void shared_obj_exit(void)
  114. {
  115.     /* If we got what we wanted, trigger the destructors, etc. in the shared objects linked to this binary. */
  116.     SHLibsInit(FALSE);
  117. }
  118.  
  119. void shared_obj_init()
  120. {
  121.     SHLibsInit(TRUE);
  122. }
  123.  
  124. void _init(void (*__CTOR_LIST__[])(void))
  125. {
  126.     int num_ctors, i;
  127.     int j;
  128.  
  129.     for (i = 1, num_ctors = 0; __CTOR_LIST__[i] != NULL; i++)
  130.         num_ctors++;
  131.  
  132.     for (j = 0; j < num_ctors; j++)
  133.     {
  134.         __CTOR_LIST__[num_ctors - j]();
  135.     }
  136. }
  137.  
  138. void _fini(void (*__DTOR_LIST__[])(void))
  139. {
  140.     int num_dtors, i;
  141.     static int j;
  142.     extern void shared_obj_exit(void);
  143.  
  144.     for (i = 1, num_dtors = 0; __DTOR_LIST__[i] != NULL; i++)
  145.         num_dtors++;
  146.  
  147.     while (j++ < num_dtors)
  148.         __DTOR_LIST__[j]();
  149.  
  150.     /* The shared objects need to be cleaned up after all local
  151.        destructors have been invoked. */
  152.     shared_obj_exit();
  153. }
  154.  
  155. int
  156. _start(char *args, int arglen, struct ExecBase *sysbase)
  157. {
  158.     int result = -1;
  159.     extern void shared_obj_init(void);
  160.  
  161.     /* Try to open the libraries we need to proceed. */
  162.     if (CANNOT open_libraries(sysbase))
  163.     {
  164.         const char *error_message;
  165.  
  166.         /* If available, use the error message provided by the client. */
  167.         error_message = __minimum_os_lib_error;
  168.  
  169.         if (error_message == NULL)
  170.             error_message = "This program requires AmigaOS 4.1 or higher.";
  171.  
  172.         __show_error(error_message);
  173.         goto out;
  174.     }
  175.  
  176.     /* The shared objects need to be set up before any local
  177.        constructors are invoked. */
  178.     shared_obj_init();
  179.  
  180.     result = _main();
  181.  
  182. out:
  183.     close_libraries();
  184.     return result;
  185. }
  186.  
  187. static BOOL
  188. open_libraries(struct ExecBase *sysbase)
  189. {
  190.     BOOL success = FALSE;
  191.  
  192.     /* Get exec interface */
  193.     IExec = (struct ExecIFace *)((struct ExecBase *)sysbase)->MainInterface;
  194.  
  195.     /* Open the minimum required libraries. */
  196.     DOSBase = (struct Library *)OpenLibrary("dos.library", 54);
  197.     if (DOSBase == NULL)
  198.         goto out;
  199.  
  200.     __UtilityBase = OpenLibrary("utility.library", 54);
  201.     if (__UtilityBase == NULL)
  202.         goto out;
  203.  
  204.     /* Obtain the interfaces for these libraries. */
  205.     IDOS = (struct DOSIFace *)GetInterface(DOSBase, "main", 1, 0);
  206.     if (IDOS == NULL)
  207.         goto out;
  208.  
  209.     __IUtility = (struct UtilityIFace *)GetInterface(__UtilityBase, "main", 1, 0);
  210.     if (__IUtility == NULL)
  211.         goto out;
  212.  
  213.     /* We need elf.library V52.2 or higher. */
  214.     ElfBase = OpenLibrary("elf.library", 0);
  215.     if (ElfBase == NULL || (ElfBase->lib_Version < 52) || (ElfBase->lib_Version == 52 && ElfBase->lib_Revision < 2))
  216.         goto out;
  217.  
  218.     IElf = (struct ElfIFace *)GetInterface(ElfBase, "main", 1, NULL);
  219.     if (IElf == NULL)
  220.         goto out;
  221.  
  222.     success = TRUE;
  223.  
  224. out:
  225.  
  226.     return (success);
  227. }
  228.  
  229. static void close_libraries(void) {
  230.  
  231.     if (IDOS != NULL)
  232.     {
  233.         DropInterface((struct Interface *)IDOS);
  234.         IDOS = NULL;
  235.     }
  236.  
  237.     if (DOSBase != NULL)
  238.     {
  239.         CloseLibrary(DOSBase);
  240.         DOSBase = NULL;
  241.     }
  242.  
  243.     if (IElf != NULL)
  244.     {
  245.         DropInterface((struct Interface *)IElf);
  246.         IElf = NULL;
  247.     }
  248.  
  249.     if (ElfBase != NULL)
  250.     {
  251.         CloseLibrary(ElfBase);
  252.         ElfBase = NULL;
  253.     }
  254. }
Advertisement
RAW Paste Data Copied
Advertisement