/**************************************************************************************
 Copyright (c) 2002 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: loadlibrary.c,v 1.3 2003/05/11 23:37:07 reddawg Exp $

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

#include <ld/ld.h>
#include <ubixos/file.h>
#include <ubixos/syserr.h>
#include <ubixos/elf.h>
#include <ubixos/vitals.h>
#include <lib/kmalloc.h>
#include <vmm/paging.h>

void loadLibrary(char *libraryName) {
  uInt16 i = 0x0;
  void   *tmp = 0x0;
  fileDescriptor        *fd            = 0x0;
  elfHeader             *eHeader       = 0x0;
  elfProgramheader      *programHeader = 0x0;
  elfSectionheader      *sectionHeader = 0x0;
  sharedLib             *tmpLib        = 0x0;
  
  if ((fd = fopen(libraryName,"rb")) == 0x0) {
    sysErr(fileErr,"Couldn't Open A System File");
    return;
    }
  eHeader = (elfHeader *)kmalloc(sizeof(elfHeader),-2);

  fread(eHeader,sizeof(elfHeader),1,fd);

  if ((eHeader->eIdent[1] != 'E') && (eHeader->eIdent[2] != 'L') && (eHeader->eIdent[3] != 'F')) {
    sysErr(systemErr,"Format Error: Binary File Not ELF32.\n");
    kfree(eHeader);
    fclose(fd);
    return;
    }
  else if (eHeader->eType != 3) {
    sysErr(systemErr,"Format Error: Binary File Not Shared Object\n");
    kfree(eHeader);
    fclose(fd); 
    return;
    }

  tmpLib = (sharedLib *)kmalloc(sizeof(sharedLib),-2);
  tmpLib->baseAddr = getFreeKernelPage(-2,((fd->size+4095)/4096));

  programHeader = (elfProgramheader *)kmalloc(sizeof(elfProgramheader)*eHeader->ePhnum,-2);
  fseek(fd,eHeader->ePhoff,0);
  fread(programHeader,sizeof(elfProgramheader)*eHeader->ePhnum,1,fd);

  sectionHeader = (elfSectionheader *)kmalloc(sizeof(elfSectionheader)*eHeader->eShnum,-2);
  fseek(fd,eHeader->eShoff,0);
  fread(sectionHeader,sizeof(elfSectionheader)*eHeader->eShnum,1,fd);

  for (i = 0x0;i < eHeader->ePhnum;i++) {
    switch (programHeader[i].phType) {
      case PT_LOAD:
        tmp = tmpLib->baseAddr + programHeader[i].phVaddr;
        kprintf("tmp: [0x%X]\n",(uInt32)tmp);
        fseek(fd,programHeader[i].phOffset,0);
        fread(tmp,programHeader[i].phFilesz,1,fd);
        break;
      case PT_DYNAMIC:
        tmp = tmpLib->baseAddr + programHeader[i].phVaddr;
        fseek(fd,programHeader[i].phOffset,0);
        fread(tmp,programHeader[i].phFilesz,1,fd);
        break;
      default:
        sysErr(systemErr,"Invalid Program Header Type");
        break;
      }
    }

  kfree(eHeader);
  kfree(programHeader);
  kfree(sectionHeader);
  
  return;
  }