ecoff_object.cc revision 7676:92274350b953
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 <string>
32
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
39// Only alpha will be able to load ecoff files for now.
40// base/types.hh and ecoff_machdep.h must be before the other .h files
41// because they are are gathered from other code bases and require some
42// typedefs from those files.
43#include "arch/alpha/ecoff_machdep.h"
44#include "base/loader/coff_sym.h"
45#include "base/loader/coff_symconst.h"
46#include "base/loader/exec_ecoff.h"
47
48using namespace std;
49
50ObjectFile *
51EcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
52{
53    if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
54        // it's Alpha ECOFF
55        return new EcoffObject(fname, fd, len, data,
56                               ObjectFile::Alpha, ObjectFile::Tru64);
57    }
58    else {
59        return NULL;
60    }
61}
62
63
64EcoffObject::EcoffObject(const string &_filename, int _fd,
65                         size_t _len, uint8_t *_data,
66                         Arch _arch, OpSys _opSys)
67    : ObjectFile(_filename, _fd, _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
92
93bool
94EcoffObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
95{
96    if (!symtab)
97        return false;
98
99    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
100        warn("loadGlobalSymbols: wrong magic on %s\n", filename);
101        return false;
102    }
103
104    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
105    if (syms->magic != magicSym2) {
106        warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
107        return false;
108    }
109
110    ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset);
111
112    char *ext_strings = (char *)(fileData + syms->cbSsExtOffset);
113    for (int i = 0; i < syms->iextMax; i++) {
114        ecoff_sym *entry = &(ext_syms[i].asym);
115        if (entry->iss != -1)
116            symtab->insert(entry->value, ext_strings + entry->iss);
117    }
118
119    return true;
120}
121
122bool
123EcoffObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
124{
125    if (!symtab)
126        return false;
127
128    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
129        warn("loadGlobalSymbols: wrong magic on %s\n", filename);
130        return false;
131    }
132
133    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
134    if (syms->magic != magicSym2) {
135        warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
136        return false;
137    }
138
139    ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset);
140    char *local_strings = (char *)(fileData + syms->cbSsOffset);
141    ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset);
142
143    for (int i = 0; i < syms->ifdMax; i++) {
144        ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase);
145        char *strings = (char *)(local_strings + fdesc[i].issBase);
146        for (int j = 0; j < fdesc[i].csym; j++) {
147            if (entry[j].st == stGlobal || entry[j].st == stProc)
148                if (entry[j].iss != -1)
149                    symtab->insert(entry[j].value, strings + entry[j].iss);
150        }
151    }
152
153    for (int i = 0; i < syms->isymMax; i++) {
154        ecoff_sym *entry = &(local_syms[i]);
155        if (entry->st == stProc)
156            symtab->insert(entry->value, local_strings + entry->iss);
157    }
158
159    return true;
160}
161