more changes for subtick labels.
[gem5.git] / base / loader / ecoff_object.cc
1 /*
2 * Copyright (c) 2003-2005 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 <string>
30
31 #include "base/loader/ecoff_object.hh"
32
33 #include "mem/functional/functional.hh"
34 #include "base/loader/symtab.hh"
35
36 #include "base/trace.hh" // for DPRINTF
37
38 #include "base/loader/exec_ecoff.h"
39 #include "base/loader/coff_sym.h"
40 #include "base/loader/coff_symconst.h"
41
42 using namespace std;
43
44 ObjectFile *
45 EcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
46 {
47 if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
48 // it's Alpha ECOFF
49 return new EcoffObject(fname, fd, len, data,
50 ObjectFile::Alpha, ObjectFile::Tru64);
51 }
52 else {
53 return NULL;
54 }
55 }
56
57
58 EcoffObject::EcoffObject(const string &_filename, int _fd,
59 size_t _len, uint8_t *_data,
60 Arch _arch, OpSys _opSys)
61 : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
62 {
63 execHdr = (ecoff_exechdr *)fileData;
64 fileHdr = &(execHdr->f);
65 aoutHdr = &(execHdr->a);
66
67 entry = aoutHdr->entry;
68
69 text.baseAddr = aoutHdr->text_start;
70 text.size = aoutHdr->tsize;
71
72 data.baseAddr = aoutHdr->data_start;
73 data.size = aoutHdr->dsize;
74
75 bss.baseAddr = aoutHdr->bss_start;
76 bss.size = aoutHdr->bsize;
77
78 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
79 text.baseAddr, text.size, data.baseAddr, data.size,
80 bss.baseAddr, bss.size);
81 }
82
83
84 bool
85 EcoffObject::loadSections(FunctionalMemory *mem, bool loadPhys)
86 {
87 Addr textAddr = text.baseAddr;
88 Addr dataAddr = data.baseAddr;
89
90 if (loadPhys) {
91 textAddr &= (ULL(1) << 40) - 1;
92 dataAddr &= (ULL(1) << 40) - 1;
93 }
94
95 // Since we don't really have an MMU and all memory is
96 // zero-filled, there's no need to set up the BSS segment.
97 mem->prot_write(textAddr, fileData + ECOFF_TXTOFF(execHdr), text.size);
98 mem->prot_write(dataAddr, fileData + ECOFF_DATOFF(execHdr), data.size);
99
100 return true;
101 }
102
103
104 bool
105 EcoffObject::loadGlobalSymbols(SymbolTable *symtab)
106 {
107 if (!symtab)
108 return false;
109
110 if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
111 warn("loadGlobalSymbols: wrong magic on %s\n", filename);
112 return false;
113 }
114
115 ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
116 if (syms->magic != magicSym2) {
117 warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
118 return false;
119 }
120
121 ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset);
122
123 char *ext_strings = (char *)(fileData + syms->cbSsExtOffset);
124 for (int i = 0; i < syms->iextMax; i++) {
125 ecoff_sym *entry = &(ext_syms[i].asym);
126 if (entry->iss != -1)
127 symtab->insert(entry->value, ext_strings + entry->iss);
128 }
129
130 return true;
131 }
132
133 bool
134 EcoffObject::loadLocalSymbols(SymbolTable *symtab)
135 {
136 if (!symtab)
137 return false;
138
139 if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
140 warn("loadGlobalSymbols: wrong magic on %s\n", filename);
141 return false;
142 }
143
144 ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
145 if (syms->magic != magicSym2) {
146 warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
147 return false;
148 }
149
150 ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset);
151 char *local_strings = (char *)(fileData + syms->cbSsOffset);
152 ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset);
153
154 for (int i = 0; i < syms->ifdMax; i++) {
155 ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase);
156 char *strings = (char *)(local_strings + fdesc[i].issBase);
157 for (int j = 0; j < fdesc[i].csym; j++) {
158 if (entry[j].st == stGlobal || entry[j].st == stProc)
159 if (entry[j].iss != -1)
160 symtab->insert(entry[j].value, strings + entry[j].iss);
161 }
162 }
163
164 for (int i = 0; i < syms->isymMax; i++) {
165 ecoff_sym *entry = &(local_syms[i]);
166 if (entry->st == stProc)
167 symtab->insert(entry->value, local_strings + entry->iss);
168 }
169
170 return true;
171 }