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