File moves for the reorg. Tree is in broken state until I commit the makefile and
[gem5.git] / base / loader / object_file.cc
1 /*
2 * Copyright (c) 2003 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
29 #include <list>
30 #include <string>
31
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <unistd.h>
37
38 #include "cprintf.hh"
39 #include "object_file.hh"
40 #include "symtab.hh"
41
42 #include "ecoff_object.hh"
43 #include "aout_object.hh"
44 #include "elf_object.hh"
45
46 using namespace std;
47
48 ObjectFile::ObjectFile(const string &_filename, int _fd,
49 size_t _len, uint8_t *_data)
50 : filename(_filename), descriptor(_fd), fileData(_data), len(_len)
51 {
52 }
53
54
55 ObjectFile::~ObjectFile()
56 {
57 close();
58 }
59
60
61 void
62 ObjectFile::close()
63 {
64 if (descriptor >= 0) {
65 ::close(descriptor);
66 descriptor = -1;
67 }
68
69 if (fileData) {
70 ::munmap(fileData, len);
71 fileData = NULL;
72 }
73 }
74
75
76 ObjectFile *
77 createObjectFile(const string &fname)
78 {
79 // open the file
80 int fd = open(fname.c_str(), O_RDONLY);
81 if (fd < 0) {
82 return NULL;
83 }
84
85 // find the length of the file by seeking to the end
86 size_t len = (size_t)lseek(fd, 0, SEEK_END);
87
88 // mmap the whole shebang
89 uint8_t *fileData =
90 (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
91 if (fileData == MAP_FAILED) {
92 close(fd);
93 return NULL;
94 }
95
96 ObjectFile *fileObj = NULL;
97
98 // figure out what we have here
99 if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
100 return fileObj;
101 }
102
103 if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
104 return fileObj;
105 }
106
107 if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
108 return fileObj;
109 }
110
111 // don't know what it is
112 close(fd);
113 munmap(fileData, len);
114 return NULL;
115 }