/*****************************************************************************************
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 <ubixos/types.h>
#include <ubixos/ld.h>
#include <ubixos/sched.h>
#include <ubixos/elf.h>
#include <ubixos/kpanic.h>
#include <lib/kprintf.h>
#include <lib/kmalloc.h>
#include <vfs/vfs.h>
#include <vmm/vmm.h>
#include <string.h>
#include <assert.h>
uInt32 ldEnable() {
int i = 0x0;
int x = 0x0;
int rel = 0x0;
int sym = 0x0;
char *newLoc = 0x0;
char *shStr = 0x0;
char *dynStr = 0x0;
uInt32 *reMap = 0x0;
fileDescriptor *ldFd = 0x0;
elfHeader *binaryHeader = 0x0;
elfProgramHeader *programHeader = 0x0;
elfSectionHeader *sectionHeader = 0x0;
elfDynSym *relSymTab = 0x0;
elfPltInfo *elfRel = 0x0;
ldFd = fopen("/ld.so","rb");
if (ldFd == 0x0) {
kprintf("Can not open ld.so\n");
}
fseek(ldFd,0x0,0x0);
binaryHeader = (elfHeader *)kmalloc(sizeof(elfHeader));
assert(binaryHeader);
fread(binaryHeader,sizeof(elfHeader),1,ldFd);
programHeader = (elfProgramHeader *)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum);
assert(programHeader);
fseek(ldFd,binaryHeader->ePhoff,0);
fread(programHeader,sizeof(elfSectionHeader),binaryHeader->ePhnum,ldFd);
sectionHeader = (elfSectionHeader *)kmalloc(sizeof(elfSectionHeader)*binaryHeader->eShnum);
assert(sectionHeader);
fseek(ldFd,binaryHeader->eShoff,0);
fread(sectionHeader,sizeof(elfSectionHeader),binaryHeader->eShnum,ldFd);
shStr = (char *)kmalloc(sectionHeader[binaryHeader->eShstrndx].shSize);
fseek(ldFd,sectionHeader[binaryHeader->eShstrndx].shOffset,0);
fread(shStr,sectionHeader[binaryHeader->eShstrndx].shSize,1,ldFd);
for (i=0;i<binaryHeader->ePhnum;i++) {
switch (programHeader[i].phType) {
case PT_LOAD:
case PT_DYNAMIC:
newLoc = (char *)programHeader[i].phVaddr + LD_START;
/*
Allocate Memory Im Going To Have To Make This Load Memory With Correct
Settings so it helps us in the future
*/
for (x=0;x < ((programHeader[i].phMemsz)+4095);x += 0x1000) {
/* make r/w or ro */
if ((vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + x + LD_START),PAGE_DEFAULT)) == 0x0)
kpanic("vmmRemapPage: ld\n");
memset((void *)((programHeader[i].phVaddr & 0xFFFFF000) + x + LD_START),0x0,0x1000);
}
/* Now Load Section To Memory */
fseek(ldFd,programHeader[i].phOffset,0x0);
fread(newLoc,programHeader[i].phFilesz,1,ldFd);
break;
case PT_GNU_STACK:
/* Tells us if the stack should be executable. Failsafe to executable
until we add checking */
break;
case PT_PAX_FLAGS:
/* Not sure... */
break;
default:
kprintf("Unhandled Header : %08x\n", programHeader[i].phType);
break;
}
}
for (i=0x0;i<binaryHeader->eShnum;i++) {
switch (sectionHeader[i].shType) {
case 3:
if (!strcmp((shStr + sectionHeader[i].shName),".dynstr")) {
dynStr = (char *)kmalloc(sectionHeader[i].shSize);
fseek(ldFd,sectionHeader[i].shOffset,0x0);
fread(dynStr,sectionHeader[i].shSize,1,ldFd);
}
break;
case 9:
elfRel = (elfPltInfo *)kmalloc(sectionHeader[i].shSize);
fseek(ldFd,sectionHeader[i].shOffset,0x0);
fread(elfRel,sectionHeader[i].shSize,1,ldFd);
for (x=0x0;x<sectionHeader[i].shSize/sizeof(elfPltInfo);x++) {
rel = ELF32_R_SYM(elfRel[x].pltInfo);
reMap = (uInt32 *)((uInt32)LD_START + elfRel[x].pltOffset);
switch (ELF32_R_TYPE(elfRel[x].pltInfo)) {
case R_386_32:
*reMap += ((uInt32)LD_START + relSymTab[rel].dynValue);
break;
case R_386_PC32:
*reMap += ((uInt32)LD_START + relSymTab[rel].dynValue) - (uInt32)reMap;
break;
case R_386_RELATIVE:
*reMap += (uInt32)LD_START;
break;
default:
kprintf("[0x%X][0x%X](%i)[%s]\n",elfRel[x].pltOffset,elfRel[x].pltInfo,rel,elfGetRelType(ELF32_R_TYPE(elfRel[x].pltInfo)));
kprintf("relTab [%s][0x%X][0x%X]\n",dynStr + relSymTab[rel].dynName,relSymTab[rel].dynValue,relSymTab[rel].dynName);
break;
}
}
kfree(elfRel);
break;
case 11:
relSymTab = (elfDynSym *)kmalloc(sectionHeader[i].shSize);
fseek(ldFd,sectionHeader[i].shOffset,0x0);
fread(relSymTab,sectionHeader[i].shSize,1,ldFd);
sym = i;
break;
}
}
i = binaryHeader->eEntry + LD_START;
kfree(dynStr);
kfree(shStr);
kfree(relSymTab);
kfree(sectionHeader);
kfree(programHeader);
kfree(binaryHeader);
fclose(ldFd);
return((uInt32)i);
}
/***
$Log$
Revision 1.34 2004/09/07 22:00:20 apwillia
Fix gcc 2.95 being retarded and not follwing the C99 specs
Revision 1.33 2004/07/28 15:05:43 reddawg
Major:
Pages now have strict security enforcement.
Many null dereferences have been resolved.
When apps loaded permissions set for pages rw and ro
Revision 1.32 2004/07/28 00:17:05 reddawg
Major:
Disconnected page 0x0 from the system... Unfortunately this broke many things
all of which have been fixed. This was good because nothing deferences NULL
any more.
Things affected:
malloc,kmalloc,getfreepage,getfreevirtualpage,pagefault,fork,exec,ld,ld.so,exec,file
Revision 1.31 2004/07/22 19:32:42 reddawg
absolute path
Revision 1.30 2004/07/21 10:02:09 reddawg
devfs: renamed functions
device system: renamed functions
fdc: fixed a few potential bugs and cleaned up some unused variables
strol: fixed definition
endtask: made it print out freepage debug info
kmalloc: fixed a huge memory leak we had some unhandled descriptor insertion so some descriptors were lost
ld: fixed a pointer conversion
file: cleaned up a few unused variables
sched: broke task deletion
kprintf: fixed ogPrintf definition
Revision 1.29 2004/07/20 22:29:55 reddawg
assert: remade assert
Revision 1.28 2004/07/17 03:10:18 reddawg
Added asserts no problems thusfar
Revision 1.27 2004/06/28 23:12:58 reddawg
file format now container:/path/to/file
Revision 1.26 2004/06/18 15:18:05 reddawg
bug fixes: did some double checking on pointers and 0x0 out memory
Revision 1.25 2004/06/17 13:16:55 reddawg
debug: removed all dead debug code
Revision 1.24 2004/06/17 13:05:14 reddawg
dynamic linking: fixed int6 issue problem was multiple rel's
Revision 1.23 2004/06/17 12:51:27 reddawg
try now
Revision 1.22 2004/06/17 12:43:59 reddawg
debug: show me output from this
Revision 1.21 2004/06/17 12:39:30 reddawg
ok
Revision 1.20 2004/06/17 12:35:15 reddawg
Test
Revision 1.19 2004/06/17 12:28:56 reddawg
debug: show me the output after this is a tested
Revision 1.18 2004/06/17 02:58:49 reddawg
Cleaned Out Dead Code
Revision 1.17 2004/06/17 02:19:29 reddawg
Cleaning out dead code
Revision 1.16 2004/06/17 01:52:54 reddawg
Maybe?
Revision 1.15 2004/06/17 01:23:23 reddawg
Do This
Revision 1.14 2004/06/17 01:09:25 reddawg
TCA: cvs update make and give me output
Revision 1.13 2004/06/16 19:45:11 reddawg
Cleaned Up Code
Revision 1.12 2004/06/16 17:49:02 reddawg
Fixed typo printf <--> kprintf
Revision 1.11 2004/06/16 17:46:42 reddawg
Cleaned Dead Code
Revision 1.10 2004/06/16 17:32:14 reddawg
Removed Dead LD Code now part of ld.so
Revision 1.9 2004/06/16 17:04:13 reddawg
ld.so: rest of the commit
Revision 1.6 2004/06/14 12:20:54 reddawg
notes: many bugs repaired and ld works 100% now.
Revision 1.5 2004/06/13 03:05:15 reddawg
we now have a dynamic linker
Revision 1.4 2004/06/12 01:27:26 reddawg
shared objects: yes we almost fully support shared objects
Revision 1.3 2004/05/15 02:30:28 reddawg
Lots of changes
END
***/