Subversion Repositories Open64

[/] [sim/] [fsim/] [utils/] [include/] [loader.h] - Blame information for rev 2072

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2072 malin
/*
2
 *  Copyright (c) 2006 Beijing SimpLight Nanoelectornics, Ltd.
3
 *  All rights reserved.
4
 */
5
 
6
/*
7
  Copyright (C) 2005, 2006.  Free Software Foundation, Inc.
8
 
9
  This program is free software; you can redistribute it and/or modify it
10
  under the terms of version 2 of the GNU General Public License as
11
  published by the Free Software Foundation.
12
 
13
  This program is distributed in the hope that it would be useful, but
14
  WITHOUT ANY WARRANTY; without even the implied warranty of
15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 
17
  Further, this software is distributed without any warranty that it is
18
  free of the rightful claim of any third person regarding infringement
19
  or the like.  Any license provided herein, whether implied or
20
  otherwise, applies only to this software file.  Patent licenses, if
21
  any, provided herein do not apply to combinations of this program with
22
  other software, or any other product whatsoever.
23
 
24
  You should have received a copy of the GNU General Public License along
25
  with this program; if not, write the Free Software Foundation, Inc., 59
26
  Temple Place - Suite 330, Boston MA 02111-1307, USA.
27
*/
28
 
29
 
30
#ifndef _LOADER_H_
31
#define _LOADER_H_
32
 
33
//#include "ELFIO/ELFIO.h"
34
#include <fcntl.h>
35
#include <unistd.h>
36
#include <elf.h>
37
#include "elf_reader.h"
38
#include "defs.h"
39
 
40
#define DISPLAY_ERR 1
41
#define _DEBUG_LOADER 0
42
#if _DEBUG_LOADER
43
#define DEBUG_LOADER printf
44
#else
45
#define DEBUG_LOADER(x,...) ((void)1)
46
#endif
47
 
48
template<class MMUClass>
49
class Loader {
50
  public:
51
  Loader();
52
  virtual ~Loader();
53
  ELF_object* createELFObj(STRING elfFile);
54
  INT loadELFObj(MMUClass& mmu, ELF_object *elfobj);
55
 
56
  private:
57
};
58
 
59
template<class MMUClass>
60
Loader<MMUClass>::Loader(){
61
}
62
 
63
template<class MMUClass>
64
Loader<MMUClass>::~Loader(){
65
}
66
 
67
/* Wrapper function for loading elf */
68
template<class MMUClass>
69
ELF_object* Loader<MMUClass>::createELFObj(STRING elfFile) {
70
  try{
71
 
72
    ELF_object* elfobj = new ELF_object(elfFile);
73
    return elfobj;
74
  } catch (STRING s) {
75
    throw s;
76
  }
77
}
78
 
79
template<class MMUClass>
80
INT Loader<MMUClass>::loadELFObj(MMUClass& mmu, ELF_object *elfobj) {
81
  try{
82
    INT32 i;
83
 
84
    // Write text segment to memory
85
    // write to virtual memory
86
    for (i=0; i<elfobj->progHdrs()->numExecSeg(); i++) {
87
      if (elfobj->textSegFSize(i) > 0) {
88
        mmu.writeBlock((ADDR)elfobj->textSegPAddr(i), (BYTE*)elfobj->textSegFAddr(i),
89
          (UINT)elfobj->textSegFSize(i));
90
 
91
        vmsg(VERBOSE_1, "Loading text segment %d: target addr=%#08x size=%#08x\n",
92
          i, elfobj->textSegPAddr(i), elfobj->textSegFSize(i));
93
      }
94
      if (elfobj->textSegMSize(i)-elfobj->textSegFSize(i) > 0) {
95
        mmu.initBlock((WORD)(elfobj->textSegVAddr(i) + elfobj->textSegFSize(i)),
96
            0, elfobj->textSegMSize(i) - elfobj->textSegFSize(i));
97
        vmsg(VERBOSE_1, "Clear bss in text segment: target addr=%#08x size=%#08x\n",
98
          elfobj->textSegVAddr(i) + elfobj->textSegFSize(i),
99
          elfobj->textSegMSize(i) - elfobj->textSegFSize(i));
100
      }
101
    }
102
 
103
    // Write rodata segment to memory
104
    // write to virtual memory
105
    for (i=0; i<elfobj->progHdrs()->numRSeg(); i++) {
106
      if ((UINT)elfobj->rodataSegFSize(i) > 0) {
107
        mmu.writeBlock((ADDR)elfobj->rodataSegPAddr(i), (BYTE*)elfobj->rodataSegFAddr(i),
108
            (UINT)elfobj->rodataSegMSize(i));
109
        vmsg(VERBOSE_1, "Loading rodata segment %d: target addr=%#08x size=%#08x\n",
110
          i, elfobj->rodataSegPAddr(i), elfobj->rodataSegMSize(i));
111
      }
112
    }
113
 
114
    for (i=0; i<elfobj->progHdrs()->numRWSeg(); i++) {
115
      // Write data segment to memory
116
      // write to virtual memory
117
      if ((UINT)elfobj->dataSegFSize(i) > 0) {
118
        mmu.writeBlock((ADDR)elfobj->dataSegPAddr(i), (BYTE*)elfobj->dataSegFAddr(i),
119
            (UINT)elfobj->dataSegFSize(i));
120
        vmsg(VERBOSE_1, "Loading data segment %d: target addr=%#08x size=%#08x\n",
121
          i, elfobj->dataSegPAddr(i), elfobj->dataSegFSize(i));
122
      }
123
 
124
      // BSS section inside data segment
125
      // write to virtual memory
126
      if (elfobj->dataSegMSize(i)-elfobj->dataSegFSize(i) > 0) {
127
        mmu.initBlock((WORD)(elfobj->dataSegVAddr(i) + elfobj->dataSegFSize(i)),
128
            0, elfobj->dataSegMSize(i) - elfobj->dataSegFSize(i));
129
        vmsg(VERBOSE_1, "Clear bss in data segment: target addr=%#08x size=%#08x\n",
130
          elfobj->dataSegVAddr(i) + elfobj->dataSegFSize(i),
131
          elfobj->dataSegMSize(i) - elfobj->dataSegFSize(i));
132
      }
133
    }
134
 
135
    // Clear BSS in virtual memory
136
    for (i=0; i<elfobj->progHdrs()->numBssSeg(); i++) {
137
      if (elfobj->bssSegMSize(i) > 0) {
138
        mmu.initBlock((WORD)elfobj->bssSegVAddr(i), 0, elfobj->bssSegMSize(i));
139
        vmsg(VERBOSE_1, "Clear bss segment %d: target addr=%#08x size=%#08x\n",
140
          i, elfobj->bssSegVAddr(i),  elfobj->bssSegMSize(i));
141
      }
142
    }
143
 
144
    return 0;
145
  } catch (STRING s) {
146
    throw s;
147
  }
148
  return -1;
149
}
150
 
151
#endif //_LOADER_H_