1 | /* $Id: kLdrMod.c 2827 2006-10-22 18:05:28Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * kLdr - The Dynamic Loader, The module interpreter.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kLdr.
|
---|
10 | *
|
---|
11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kLdr is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kLdr; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <kLdr.h>
|
---|
32 | #include "kLdrHlp.h"
|
---|
33 | #include "kLdrInternal.h"
|
---|
34 | #include "kLdrModMZ.h"
|
---|
35 | #if 1 /* testing headers */
|
---|
36 | # include "kLdrModPE.h"
|
---|
37 | //# include "kLdrModLX.h"
|
---|
38 | # include "kLdrModELF32.h"
|
---|
39 | # include "kLdrModELF64.h"
|
---|
40 | #endif
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Open a executable image from a file provider instance.
|
---|
45 | *
|
---|
46 | * @returns 0 on success and *ppMod pointing to a module instance.
|
---|
47 | * On failure, a non-zero OS specific error code is returned.
|
---|
48 | * @param pRdr The file provider instance to use.
|
---|
49 | * On success, the ownership of the instance is taken by the
|
---|
50 | * module and the caller must not ever touch it again.
|
---|
51 | * (The instance is not closed on failure, the call has to do that.)
|
---|
52 | * @param ppMod Where to store the module handle.
|
---|
53 | */
|
---|
54 | int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod)
|
---|
55 | {
|
---|
56 | union
|
---|
57 | {
|
---|
58 | uint32_t u32;
|
---|
59 | uint16_t u16;
|
---|
60 | uint16_t au16[2];
|
---|
61 | uint8_t au8[4];
|
---|
62 | } u;
|
---|
63 | off_t offHdr = 0;
|
---|
64 | int rc;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Try figure out what kind of image this is.
|
---|
68 | * Always read the 'new header' if we encounter MZ.
|
---|
69 | */
|
---|
70 | rc = kLdrRdrRead(pRdr, &u, sizeof(u), 0);
|
---|
71 | if (rc)
|
---|
72 | return rc;
|
---|
73 | if ( u.u16 == IMAGE_DOS_SIGNATURE
|
---|
74 | && kLdrRdrSize(pRdr) > sizeof(IMAGE_DOS_HEADER))
|
---|
75 | {
|
---|
76 | rc = kLdrRdrRead(pRdr, &u, sizeof(u.u32), KLDR_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
|
---|
77 | if (rc)
|
---|
78 | return rc;
|
---|
79 | if ((off_t)u.u32 < kLdrRdrSize(pRdr))
|
---|
80 | {
|
---|
81 | offHdr = u.u32;
|
---|
82 | rc = kLdrRdrRead(pRdr, &u, sizeof(u.u32), offHdr);
|
---|
83 | if (rc)
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 | else
|
---|
87 | u.u16 = IMAGE_DOS_SIGNATURE;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Use the magic to select the appropriate image interpreter.
|
---|
92 | */
|
---|
93 | if (u.u16 == IMAGE_DOS_SIGNATURE)
|
---|
94 | return KLDR_ERR_MZ_NOT_SUPPORTED;
|
---|
95 | else if (u.u16 == IMAGE_NE_SIGNATURE)
|
---|
96 | return KLDR_ERR_NE_NOT_SUPPORTED;
|
---|
97 | else if (u.u16 == IMAGE_LX_SIGNATURE)
|
---|
98 | return KLDR_ERR_LX_NOT_SUPPORTED;
|
---|
99 | else if (u.u16 == IMAGE_LE_SIGNATURE)
|
---|
100 | return KLDR_ERR_LE_NOT_SUPPORTED;
|
---|
101 | else if (u.u32 == IMAGE_NT_SIGNATURE)
|
---|
102 | return KLDR_ERR_PE_NOT_SUPPORTED;
|
---|
103 | else if (u.u32 == IMAGE_ELF_SIGNATURE)
|
---|
104 | return KLDR_ERR_ELF_NOT_SUPPORTED;
|
---|
105 | return KLDR_ERR_UNKNOWN_FORMAT;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Open a executable image by file name.
|
---|
111 | *
|
---|
112 | * @returns 0 on success and *ppMod pointing to a module instance.
|
---|
113 | * On failure, a non-zero OS specific error code is returned.
|
---|
114 | * @param pszFilename The filename to open.
|
---|
115 | * @param ppMod Where to store the module handle.
|
---|
116 | */
|
---|
117 | int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod)
|
---|
118 | {
|
---|
119 | /*
|
---|
120 | * Open the file using a bit provider.
|
---|
121 | */
|
---|
122 | PKLDRRDR pRdr;
|
---|
123 | int rc = kLdrRdrOpen(&pRdr, pszFilename);
|
---|
124 | if (!rc)
|
---|
125 | {
|
---|
126 | rc = kLdrModOpenFromRdr(pRdr, ppMod);
|
---|
127 | if (!rc)
|
---|
128 | return 0;
|
---|
129 | kLdrRdrClose(pRdr);
|
---|
130 | }
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 |
|
---|