ecoff_object.cc revision 360
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2003 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com */
2812855Sgabeblack@google.com
2912855Sgabeblack@google.com#include <string>
3012855Sgabeblack@google.com
3112855Sgabeblack@google.com#include "base/loader/ecoff_object.hh"
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com#include "mem/functional_mem/functional_memory.hh"
3412855Sgabeblack@google.com#include "base/loader/symtab.hh"
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com#include "base/trace.hh"	// for DPRINTF
3712855Sgabeblack@google.com
3812855Sgabeblack@google.com#include "base/loader/exec_ecoff.h"
3912855Sgabeblack@google.com#include "base/loader/coff_sym.h"
4012855Sgabeblack@google.com#include "base/loader/coff_symconst.h"
4112855Sgabeblack@google.com
4212855Sgabeblack@google.comusing namespace std;
4312855Sgabeblack@google.com
4412855Sgabeblack@google.comObjectFile *
4512855Sgabeblack@google.comEcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
4612855Sgabeblack@google.com{
4712855Sgabeblack@google.com    if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
4812855Sgabeblack@google.com        // it's Alpha ECOFF
4912855Sgabeblack@google.com        return new EcoffObject(fname, fd, len, data,
5012855Sgabeblack@google.com                               ObjectFile::Alpha, ObjectFile::Tru64);
5112855Sgabeblack@google.com    }
5212855Sgabeblack@google.com    else {
5312855Sgabeblack@google.com        return NULL;
5412855Sgabeblack@google.com    }
5512855Sgabeblack@google.com}
56
57
58EcoffObject::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
84bool
85EcoffObject::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
104bool
105EcoffObject::loadGlobalSymbols(SymbolTable *symtab)
106{
107    if (!symtab)
108        return false;
109
110    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
111        cprintf("wrong magic\n");
112        return false;
113    }
114
115    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
116    if (syms->magic != magicSym2) {
117        cprintf("bad symbol header magic\n");
118        exit(1);
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
133bool
134EcoffObject::loadLocalSymbols(SymbolTable *symtab)
135{
136    if (!symtab)
137        return false;
138
139    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
140        cprintf("wrong magic\n");
141        return false;
142    }
143
144    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
145    if (syms->magic != magicSym2) {
146        cprintf("bad symbol header magic\n");
147        exit(1);
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}
172