openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

crtb.c (4118B)


      1 /* $Id: //depot/blt/lib/crtb.c#3 $
      2 **
      3 ** Copyright 1998 Brian J. Swetland
      4 ** All rights reserved.
      5 ** Copyright 1998-1999 Sidney Cammeresi
      6 ** All rights reserved.
      7 **
      8 ** Redistribution and use in source and binary forms, with or without
      9 ** modification, are permitted provided that the following conditions
     10 ** are met:
     11 ** 1. Redistributions of source code must retain the above copyright
     12 **    notice, this list of conditions, and the following disclaimer.
     13 ** 2. Redistributions in binary form must reproduce the above copyright
     14 **    notice, this list of conditions, and the following disclaimer in the
     15 **    documentation and/or other materials provided with the distribution.
     16 ** 3. The name of the author may not be used to endorse or promote products
     17 **    derived from this software without specific prior written permission.
     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 */
     30 
     31 #include <string.h>
     32 #include <elf.h>
     33 #include <blt/syscall.h>
     34 #include <blt/libsyms.h>
     35 
     36 int main(int argc, char **argv);
     37 void __libc_init_memory(unsigned int top_of_binary,
     38                         unsigned int start_bss, unsigned int bss_length);
     39 
     40 static char *strtab = NULL;
     41 static int symtablen = 0, top = 0;
     42 static elf32_hdr_t *hdr = NULL;
     43 static elf32_sym_t *symtab = NULL;
     44 static void run_all (const char *name);
     45 
     46 void _start(int argc, char **argv)
     47 {
     48 	elf32_sec_hdr_t *last;
     49 
     50 	hdr = (elf32_hdr_t *) 0x1000;
     51 	symtab = _elf_find_section_data (hdr, ".symtab");
     52 	strtab = _elf_find_section_data (hdr, ".strtab");
     53 	symtablen = _elf_section_size (hdr, ".symtab") / sizeof (elf32_sym_t);
     54 	last = (elf32_sec_hdr_t *) ((unsigned int) hdr + hdr->e_shoff +
     55 		hdr->e_shentsize * (hdr->e_shnum - 1));
     56 	top = (unsigned int) hdr + last->sh_offset + last->sh_size;
     57 	
     58 	__libc_init_memory((unsigned int) top, (unsigned int)
     59 		_elf_find_section_data (hdr, ".bss"), _elf_section_size (hdr, ".bss"));
     60 
     61 	run_all ("_init");
     62 	os_terminate (main (argc, argv));
     63 }
     64 
     65 static void run_all (const char *name)
     66 {
     67 	int i;
     68 
     69 	for (i = 0; i < symtablen; i++)
     70 		if (!strcmp (strtab + symtab[i].st_name, name) &&
     71 				(symtab[i].st_shndx != SHN_UNDEF))
     72 			(*((void (*)(void)) symtab[i].st_value)) ();
     73 }
     74 
     75 elf32_sec_hdr_t *_elf_find_section_hdr (elf32_hdr_t *hdr, char *name)
     76 {
     77 	char *section_name;
     78 	int i;
     79 	elf32_sec_hdr_t *sec_hdr;
     80 
     81 	sec_hdr = (elf32_sec_hdr_t *) ((unsigned int) hdr + hdr->e_shoff +
     82 	    hdr->e_shstrndx * hdr->e_shentsize);
     83 	section_name = (char *) ((unsigned int) hdr + sec_hdr->sh_offset);
     84 	sec_hdr = (elf32_sec_hdr_t *) ((unsigned int) hdr + hdr->e_shoff);
     85 	for (i = 0; i < hdr->e_shnum; i++, sec_hdr = (elf32_sec_hdr_t *)
     86 	        ((unsigned int) sec_hdr + hdr->e_shentsize))
     87 	    if (!strcmp (section_name + sec_hdr->sh_name, name))
     88 	        return sec_hdr;
     89 	return NULL;
     90 }
     91 
     92 void *_elf_find_section_data (elf32_hdr_t *hdr, char *name)
     93 {
     94 	elf32_sec_hdr_t *sec_hdr;
     95 
     96 	sec_hdr = _elf_find_section_hdr (hdr, name);
     97 	return (sec_hdr == NULL) ? NULL : (void *) ((unsigned int) hdr +
     98 	    sec_hdr->sh_offset);
     99 }
    100 
    101 int _elf_section_size (elf32_hdr_t *hdr, char *name)
    102 {
    103 	elf32_sec_hdr_t *sec_hdr;
    104 
    105 	sec_hdr = _elf_find_section_hdr (hdr, name);
    106 	return (sec_hdr == NULL) ? 0 : sec_hdr->sh_size;
    107 }
    108 
    109 /****** this crap is to make ld happy when we build shared executables ******/
    110 
    111 int ___brk_addr;
    112 int __environ;
    113 
    114 void __attribute__ ((noreturn)) abort ()
    115 {
    116 	for (;;) ;
    117 }
    118 
    119 void atexit ()
    120 {
    121 }
    122 
    123 /********************************* end crap *********************************/
    124