object_file.cc (2632:1bb2f91485ea) object_file.cc (2665:a124942bacb8)
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.
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
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 "base/cprintf.hh"
39#include "base/loader/object_file.hh"
40#include "base/loader/symtab.hh"
41
42#include "base/loader/ecoff_object.hh"
43#include "base/loader/aout_object.hh"
44#include "base/loader/elf_object.hh"
45
46#include "mem/translating_port.hh"
47
48using namespace std;
49
50ObjectFile::ObjectFile(const string &_filename, int _fd,
51 size_t _len, uint8_t *_data,
52 Arch _arch, OpSys _opSys)
53 : filename(_filename), descriptor(_fd), fileData(_data), len(_len),
54 arch(_arch), opSys(_opSys)
55{
56}
57
58
59ObjectFile::~ObjectFile()
60{
61 close();
62}
63
64
65bool
66ObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask)
67{
68 if (sec->size != 0) {
69 Addr addr = sec->baseAddr & addrMask;
70 if (sec->fileImage) {
71 memPort->writeBlob(addr, sec->fileImage, sec->size);
72 }
73 else {
74 // no image: must be bss
75 memPort->memsetBlob(addr, 0, sec->size);
76 }
77 }
78 return true;
79}
80
81
82bool
83ObjectFile::loadSections(Port *memPort, Addr addrMask)
84{
85 return (loadSection(&text, memPort, addrMask)
86 && loadSection(&data, memPort, addrMask)
87 && loadSection(&bss, memPort, addrMask));
88}
89
90
91void
92ObjectFile::close()
93{
94 if (descriptor >= 0) {
95 ::close(descriptor);
96 descriptor = -1;
97 }
98
99 if (fileData) {
100 ::munmap(fileData, len);
101 fileData = NULL;
102 }
103}
104
105
106ObjectFile *
107createObjectFile(const string &fname)
108{
109 // open the file
110 int fd = open(fname.c_str(), O_RDONLY);
111 if (fd < 0) {
112 return NULL;
113 }
114
115 // find the length of the file by seeking to the end
116 size_t len = (size_t)lseek(fd, 0, SEEK_END);
117
118 // mmap the whole shebang
119 uint8_t *fileData =
120 (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
121 if (fileData == MAP_FAILED) {
122 close(fd);
123 return NULL;
124 }
125
126 ObjectFile *fileObj = NULL;
127
128 // figure out what we have here
129 if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
130 return fileObj;
131 }
132
133 if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
134 return fileObj;
135 }
136
137 if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
138 return fileObj;
139 }
140
141 // don't know what it is
142 close(fd);
143 munmap(fileData, len);
144 return NULL;
145}
30 */
31
32#include <list>
33#include <string>
34
35#include <sys/types.h>
36#include <sys/mman.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <unistd.h>
40
41#include "base/cprintf.hh"
42#include "base/loader/object_file.hh"
43#include "base/loader/symtab.hh"
44
45#include "base/loader/ecoff_object.hh"
46#include "base/loader/aout_object.hh"
47#include "base/loader/elf_object.hh"
48
49#include "mem/translating_port.hh"
50
51using namespace std;
52
53ObjectFile::ObjectFile(const string &_filename, int _fd,
54 size_t _len, uint8_t *_data,
55 Arch _arch, OpSys _opSys)
56 : filename(_filename), descriptor(_fd), fileData(_data), len(_len),
57 arch(_arch), opSys(_opSys)
58{
59}
60
61
62ObjectFile::~ObjectFile()
63{
64 close();
65}
66
67
68bool
69ObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask)
70{
71 if (sec->size != 0) {
72 Addr addr = sec->baseAddr & addrMask;
73 if (sec->fileImage) {
74 memPort->writeBlob(addr, sec->fileImage, sec->size);
75 }
76 else {
77 // no image: must be bss
78 memPort->memsetBlob(addr, 0, sec->size);
79 }
80 }
81 return true;
82}
83
84
85bool
86ObjectFile::loadSections(Port *memPort, Addr addrMask)
87{
88 return (loadSection(&text, memPort, addrMask)
89 && loadSection(&data, memPort, addrMask)
90 && loadSection(&bss, memPort, addrMask));
91}
92
93
94void
95ObjectFile::close()
96{
97 if (descriptor >= 0) {
98 ::close(descriptor);
99 descriptor = -1;
100 }
101
102 if (fileData) {
103 ::munmap(fileData, len);
104 fileData = NULL;
105 }
106}
107
108
109ObjectFile *
110createObjectFile(const string &fname)
111{
112 // open the file
113 int fd = open(fname.c_str(), O_RDONLY);
114 if (fd < 0) {
115 return NULL;
116 }
117
118 // find the length of the file by seeking to the end
119 size_t len = (size_t)lseek(fd, 0, SEEK_END);
120
121 // mmap the whole shebang
122 uint8_t *fileData =
123 (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
124 if (fileData == MAP_FAILED) {
125 close(fd);
126 return NULL;
127 }
128
129 ObjectFile *fileObj = NULL;
130
131 // figure out what we have here
132 if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
133 return fileObj;
134 }
135
136 if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
137 return fileObj;
138 }
139
140 if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
141 return fileObj;
142 }
143
144 // don't know what it is
145 close(fd);
146 munmap(fileData, len);
147 return NULL;
148}