includes: sort all includes
[gem5.git] / src / base / loader / object_file.cc
1 /*
2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
32 #include <sys/mman.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #include <cstdio>
38 #include <list>
39 #include <string>
40
41 #include "base/loader/aout_object.hh"
42 #include "base/loader/ecoff_object.hh"
43 #include "base/loader/elf_object.hh"
44 #include "base/loader/object_file.hh"
45 #include "base/loader/raw_object.hh"
46 #include "base/loader/symtab.hh"
47 #include "base/cprintf.hh"
48 #include "mem/translating_port.hh"
49
50 using namespace std;
51
52 ObjectFile::ObjectFile(const string &_filename, int _fd,
53 size_t _len, uint8_t *_data,
54 Arch _arch, OpSys _opSys)
55 : filename(_filename), descriptor(_fd), fileData(_data), len(_len),
56 arch(_arch), opSys(_opSys)
57 {
58 }
59
60
61 ObjectFile::~ObjectFile()
62 {
63 close();
64 }
65
66
67 bool
68 ObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask)
69 {
70 if (sec->size != 0) {
71 Addr addr = sec->baseAddr & addrMask;
72 if (sec->fileImage) {
73 memPort->writeBlob(addr, sec->fileImage, sec->size);
74 }
75 else {
76 // no image: must be bss
77 memPort->memsetBlob(addr, 0, sec->size);
78 }
79 }
80 return true;
81 }
82
83
84 bool
85 ObjectFile::loadSections(Port *memPort, Addr addrMask)
86 {
87 return (loadSection(&text, memPort, addrMask)
88 && loadSection(&data, memPort, addrMask)
89 && loadSection(&bss, memPort, addrMask));
90 }
91
92
93 void
94 ObjectFile::close()
95 {
96 if (descriptor >= 0) {
97 ::close(descriptor);
98 descriptor = -1;
99 }
100
101 if (fileData) {
102 ::munmap((char*)fileData, len);
103 fileData = NULL;
104 }
105 }
106
107
108 ObjectFile *
109 createObjectFile(const string &fname, bool raw)
110 {
111 // open the file
112 int fd = open(fname.c_str(), O_RDONLY);
113 if (fd < 0) {
114 return NULL;
115 }
116
117 // find the length of the file by seeking to the end
118 size_t len = (size_t)lseek(fd, 0, SEEK_END);
119
120 // mmap the whole shebang
121 uint8_t *fileData =
122 (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
123 if (fileData == MAP_FAILED) {
124 close(fd);
125 return NULL;
126 }
127
128 ObjectFile *fileObj = NULL;
129
130 // figure out what we have here
131 if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
132 return fileObj;
133 }
134
135 if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
136 return fileObj;
137 }
138
139 if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
140 return fileObj;
141 }
142
143 if (raw)
144 return RawObject::tryFile(fname, fd, len, fileData);
145
146 // don't know what it is
147 close(fd);
148 munmap((char*)fileData, len);
149 return NULL;
150 }