ecoff_object.cc (11392:5967db4cff04) ecoff_object.cc (11793:ef606668d247)
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 * Authors: Steve Reinhardt
29 */
30
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 * Authors: Steve Reinhardt
29 */
30
31#include "base/loader/ecoff_object.hh"
32
31#include <string>
32
33#include <string>
34
33#include "base/loader/ecoff_object.hh"
34#include "base/loader/symtab.hh"
35#include "base/misc.hh"
36#include "base/trace.hh"
37#include "base/types.hh"
38#include "debug/Loader.hh"
39
40// Only alpha will be able to load ecoff files for now.
41// base/types.hh and ecoff_machdep.h must be before the other .h files
42// because they are are gathered from other code bases and require some
43// typedefs from those files.
44#include "arch/alpha/ecoff_machdep.h"
45#include "base/loader/coff_sym.h"
46#include "base/loader/coff_symconst.h"
47#include "base/loader/exec_ecoff.h"
48
49using namespace std;
50
51ObjectFile *
52EcoffObject::tryFile(const string &fname, size_t len, uint8_t *data)
53{
54 if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
55 // it's Alpha ECOFF
56 return new EcoffObject(fname, len, data,
57 ObjectFile::Alpha, ObjectFile::Tru64);
58 }
59 else {
60 return NULL;
61 }
62}
63
64
65EcoffObject::EcoffObject(const string &_filename, size_t _len, uint8_t *_data,
66 Arch _arch, OpSys _opSys)
67 : ObjectFile(_filename, _len, _data, _arch, _opSys)
68{
69 execHdr = (ecoff_exechdr *)fileData;
70 fileHdr = &(execHdr->f);
71 aoutHdr = &(execHdr->a);
72
73 entry = aoutHdr->entry;
74
75 text.baseAddr = aoutHdr->text_start;
76 text.size = aoutHdr->tsize;
77 text.fileImage = fileData + ECOFF_TXTOFF(execHdr);
78
79 data.baseAddr = aoutHdr->data_start;
80 data.size = aoutHdr->dsize;
81 data.fileImage = fileData + ECOFF_DATOFF(execHdr);
82
83 bss.baseAddr = aoutHdr->bss_start;
84 bss.size = aoutHdr->bsize;
85 bss.fileImage = NULL;
86
87 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
88 text.baseAddr, text.size, data.baseAddr, data.size,
89 bss.baseAddr, bss.size);
90}
91
92bool
93EcoffObject::loadAllSymbols(SymbolTable *symtab, Addr base, Addr offset,
94 Addr addr_mask)
95{
96 bool retval = loadGlobalSymbols(symtab, base, offset, addr_mask);
97 retval = retval && loadLocalSymbols(symtab, base, offset, addr_mask);
98 return retval;
99}
100
101bool
102EcoffObject::loadGlobalSymbols(SymbolTable *symtab, Addr base, Addr offset,
103 Addr addr_mask)
104{
105 if (!symtab)
106 return false;
107
108 if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
109 warn("loadGlobalSymbols: wrong magic on %s\n", filename);
110 return false;
111 }
112
113 ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
114 if (syms->magic != magicSym2) {
115 warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
116 return false;
117 }
118
119 ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset);
120
121 char *ext_strings = (char *)(fileData + syms->cbSsExtOffset);
122 for (int i = 0; i < syms->iextMax; i++) {
123 ecoff_sym *entry = &(ext_syms[i].asym);
124 if (entry->iss != -1)
125 symtab->insert(entry->value, ext_strings + entry->iss);
126 }
127
128 return true;
129}
130
131bool
132EcoffObject::loadLocalSymbols(SymbolTable *symtab, Addr base, Addr offset,
133 Addr addr_mask)
134{
135 if (!symtab)
136 return false;
137
138 if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
139 warn("loadGlobalSymbols: wrong magic on %s\n", filename);
140 return false;
141 }
142
143 ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
144 if (syms->magic != magicSym2) {
145 warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
146 return false;
147 }
148
149 ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset);
150 char *local_strings = (char *)(fileData + syms->cbSsOffset);
151 ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset);
152
153 for (int i = 0; i < syms->ifdMax; i++) {
154 ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase);
155 char *strings = (char *)(local_strings + fdesc[i].issBase);
156 for (int j = 0; j < fdesc[i].csym; j++) {
157 if (entry[j].st == stGlobal || entry[j].st == stProc)
158 if (entry[j].iss != -1)
159 symtab->insert(entry[j].value, strings + entry[j].iss);
160 }
161 }
162
163 for (int i = 0; i < syms->isymMax; i++) {
164 ecoff_sym *entry = &(local_syms[i]);
165 if (entry->st == stProc)
166 symtab->insert(entry->value, local_strings + entry->iss);
167 }
168
169 return true;
170}
35#include "base/loader/symtab.hh"
36#include "base/misc.hh"
37#include "base/trace.hh"
38#include "base/types.hh"
39#include "debug/Loader.hh"
40
41// Only alpha will be able to load ecoff files for now.
42// base/types.hh and ecoff_machdep.h must be before the other .h files
43// because they are are gathered from other code bases and require some
44// typedefs from those files.
45#include "arch/alpha/ecoff_machdep.h"
46#include "base/loader/coff_sym.h"
47#include "base/loader/coff_symconst.h"
48#include "base/loader/exec_ecoff.h"
49
50using namespace std;
51
52ObjectFile *
53EcoffObject::tryFile(const string &fname, size_t len, uint8_t *data)
54{
55 if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
56 // it's Alpha ECOFF
57 return new EcoffObject(fname, len, data,
58 ObjectFile::Alpha, ObjectFile::Tru64);
59 }
60 else {
61 return NULL;
62 }
63}
64
65
66EcoffObject::EcoffObject(const string &_filename, size_t _len, uint8_t *_data,
67 Arch _arch, OpSys _opSys)
68 : ObjectFile(_filename, _len, _data, _arch, _opSys)
69{
70 execHdr = (ecoff_exechdr *)fileData;
71 fileHdr = &(execHdr->f);
72 aoutHdr = &(execHdr->a);
73
74 entry = aoutHdr->entry;
75
76 text.baseAddr = aoutHdr->text_start;
77 text.size = aoutHdr->tsize;
78 text.fileImage = fileData + ECOFF_TXTOFF(execHdr);
79
80 data.baseAddr = aoutHdr->data_start;
81 data.size = aoutHdr->dsize;
82 data.fileImage = fileData + ECOFF_DATOFF(execHdr);
83
84 bss.baseAddr = aoutHdr->bss_start;
85 bss.size = aoutHdr->bsize;
86 bss.fileImage = NULL;
87
88 DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
89 text.baseAddr, text.size, data.baseAddr, data.size,
90 bss.baseAddr, bss.size);
91}
92
93bool
94EcoffObject::loadAllSymbols(SymbolTable *symtab, Addr base, Addr offset,
95 Addr addr_mask)
96{
97 bool retval = loadGlobalSymbols(symtab, base, offset, addr_mask);
98 retval = retval && loadLocalSymbols(symtab, base, offset, addr_mask);
99 return retval;
100}
101
102bool
103EcoffObject::loadGlobalSymbols(SymbolTable *symtab, Addr base, Addr offset,
104 Addr addr_mask)
105{
106 if (!symtab)
107 return false;
108
109 if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
110 warn("loadGlobalSymbols: wrong magic on %s\n", filename);
111 return false;
112 }
113
114 ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
115 if (syms->magic != magicSym2) {
116 warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
117 return false;
118 }
119
120 ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset);
121
122 char *ext_strings = (char *)(fileData + syms->cbSsExtOffset);
123 for (int i = 0; i < syms->iextMax; i++) {
124 ecoff_sym *entry = &(ext_syms[i].asym);
125 if (entry->iss != -1)
126 symtab->insert(entry->value, ext_strings + entry->iss);
127 }
128
129 return true;
130}
131
132bool
133EcoffObject::loadLocalSymbols(SymbolTable *symtab, Addr base, Addr offset,
134 Addr addr_mask)
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}