Newer
Older
ubix2 / src / bin / ld / main.c
@reddawg reddawg on 15 Oct 2006 6 KB LD Loads Multiple LIBS
/*****************************************************************************************
 Copyright (c) 2002-2004 The UbixOS Project
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the UbixOS Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id$

*****************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "ld.h"

ldLibrary *libs = 0x0;
int               lib_c               = 0x0;
int               lib_s[10]; 

static elfHeader        *binaryHeader        = 0x0;
static elfSectionHeader *binarySectionHeader = 0x0;
static char             *binaryShStr         = 0x0;
static char             *binaryDynStr        = 0x0;
static elfDynSym        *binaryRelSymTab     = 0x0;
static elfPltInfo       *binaryElfRel        = 0x0;
static Elf32_Dyn        *binaryElf32_Dyn     = 0x0;

uInt32 ld(uInt32 got2,uInt32 entry) {
  int  i             = 0x0;
  int  x             = 0x0;
  int  rel           = 0x0;
  uInt32 *reMap      = 0x0;
  FILE *binaryFd     = 0x0;

  if (binaryHeader == 0x0) {
    binaryFd = malloc(sizeof(FILE));
    binaryFd->fd = (uInt32)got2;
    fseek(binaryFd,0x0,0x0);
    binaryHeader = (elfHeader *)malloc(sizeof(elfHeader));
    fread(binaryHeader,sizeof(elfHeader),1,binaryFd);
    }

  if (binarySectionHeader == 0x0) {
    binarySectionHeader = (elfSectionHeader *)malloc(sizeof(elfSectionHeader)*binaryHeader->eShnum);
    fseek(binaryFd,binaryHeader->eShoff,0);
    fread(binarySectionHeader,sizeof(elfSectionHeader),binaryHeader->eShnum,binaryFd);

  if (binaryShStr == 0x0) {
    binaryShStr = (char *)malloc(binarySectionHeader[binaryHeader->eShstrndx].shSize);
    fseek(binaryFd,binarySectionHeader[binaryHeader->eShstrndx].shOffset,0);
    fread(binaryShStr,binarySectionHeader[binaryHeader->eShstrndx].shSize,1,binaryFd);
    }

    for (i=0x0;i<binaryHeader->eShnum;i++) {
      switch (binarySectionHeader[i].shType) {
        case 3:
          if (!strcmp((binaryShStr + binarySectionHeader[i].shName),".dynstr")) {
            if (binaryDynStr == 0x0) {
              binaryDynStr = (char *)malloc(binarySectionHeader[i].shSize);
              fseek(binaryFd,binarySectionHeader[i].shOffset,0);
              fread(binaryDynStr,binarySectionHeader[i].shSize,1,binaryFd);
              }
            }
          break;
        case SHT_DYNAMIC:
          binaryElf32_Dyn = (Elf32_Dyn *)malloc(binarySectionHeader[i].shSize);
          fseek(binaryFd,binarySectionHeader[i].shOffset,0);
          fread(binaryElf32_Dyn,binarySectionHeader[i].shSize,1,binaryFd);
          for (x = 0;x < binarySectionHeader[i].shSize / sizeof(Elf32_Dyn);x++) {
            if (binaryElf32_Dyn[x].d_tag == 1) {
              lib_s[lib_c] = binaryElf32_Dyn[x].d_un.d_ptr;
              lib_c++;
              }
            }
          break;
        case 9:
          rel = i;
          break;
        case 11:
          if (binaryRelSymTab == 0x0) {
            binaryRelSymTab = (elfDynSym *)malloc(binarySectionHeader[i].shSize);
            fseek(binaryFd,binarySectionHeader[i].shOffset,0);
            fread(binaryRelSymTab,binarySectionHeader[i].shSize,1,binaryFd);
            }
          break;
        }
      }
    }

  if (binaryElfRel == 0x0) {
    fseek(binaryFd,binarySectionHeader[rel].shOffset,0x0);
    binaryElfRel = (elfPltInfo *)malloc(binarySectionHeader[rel].shSize);
    fread(binaryElfRel,binarySectionHeader[rel].shSize,1,binaryFd);
    }

  i = (entry/sizeof(elfPltInfo));
  x = ELF32_R_SYM(binaryElfRel[i].pltInfo);
  reMap = (uInt32 *)binaryElfRel[i].pltOffset;
  *reMap = ldFindFunc(binaryDynStr + binaryRelSymTab[x].dynName,binaryDynStr);
  //*reMap = ldFindFunc(binaryDynStr + binaryRelSymTab[x].dynName,(char *)(binaryDynStr + 1));

  if (binaryFd) {
    fclose(binaryFd);
    }

  return(*reMap);
  }

/***
 $Log$
 Revision 1.1.1.1  2006/06/01 12:46:09  reddawg
 ubix2

 Revision 1.2  2005/10/12 00:13:28  reddawg
 Removed

 Revision 1.1.1.1  2005/09/26 17:14:01  reddawg
 no message

 Revision 1.13  2004/06/18 15:18:04  reddawg
 bug fixes: did some double checking on pointers and 0x0 out memory

 Revision 1.12  2004/06/17 14:14:44  reddawg
 Fixed some potential problems

 Revision 1.11  2004/06/17 13:05:14  reddawg
 dynamic linking: fixed int6 issue problem was multiple rel's

 Revision 1.10  2004/06/17 12:32:11  reddawg
 the machine should just hlt

 Revision 1.9  2004/06/17 11:58:10  reddawg
 ld.so: Made a few changes to the way it functions hopefully it will
        improve some of its performance.

 Revision 1.8  2004/06/17 02:58:49  reddawg
 Cleaned Out Dead Code

 Revision 1.7  2004/06/17 01:09:24  reddawg
 TCA: cvs update make and give me output

 Revision 1.6  2004/06/16 23:11:34  reddawg
 ld.so: now adds librarys it is a little more efficient does the lazy binding

 Revision 1.5  2004/06/16 18:04:15  reddawg
 Fixed typo printf <--> kprintf

 Revision 1.4  2004/06/16 16:31:58  reddawg
 ld.so: the dynamic linker works and has been tested

 Revision 1.3  2004/06/16 13:52:51  reddawg
 Start of userland LD

 Revision 1.2  2004/06/01 01:30:43  reddawg
 No more warnings and organized make files

 Revision 1.1  2004/04/26 21:16:03  reddawg
 Initial addition of the ubix LD

 Revision 1.2  2004/04/26 13:20:42  reddawg
 Turn off muffin

 Revision 1.1  2004/04/26 13:13:25  reddawg
 Initial Introduction of the UbixOS Format Utility into the source tree

 END
 ***/