112SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
312SN/A * All rights reserved.
412SN/A *
512SN/A * Redistribution and use in source and binary forms, with or without
612SN/A * modification, are permitted provided that the following conditions are
712SN/A * met: redistributions of source code must retain the above copyright
812SN/A * notice, this list of conditions and the following disclaimer;
912SN/A * redistributions in binary form must reproduce the above copyright
1012SN/A * notice, this list of conditions and the following disclaimer in the
1112SN/A * documentation and/or other materials provided with the distribution;
1212SN/A * neither the name of the copyright holders nor the names of its
1312SN/A * contributors may be used to endorse or promote products derived from
1412SN/A * this software without specific prior written permission.
1512SN/A *
1612SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
2912SN/A */
3012SN/A
3111793Sbrandon.potter@amd.com#include "base/loader/ecoff_object.hh"
3211793Sbrandon.potter@amd.com
3312SN/A#include <string>
3412SN/A
357676Snate@binkert.org#include "base/loader/symtab.hh"
3612334Sgabeblack@google.com#include "base/logging.hh"
377676Snate@binkert.org#include "base/trace.hh"
387676Snate@binkert.org#include "base/types.hh"
398232Snate@binkert.org#include "debug/Loader.hh"
4012SN/A
417676Snate@binkert.org// Only alpha will be able to load ecoff files for now.
427676Snate@binkert.org// base/types.hh and ecoff_machdep.h must be before the other .h files
4311320Ssteve.reinhardt@amd.com// because they are are gathered from other code bases and require some
447676Snate@binkert.org// typedefs from those files.
457676Snate@binkert.org#include "arch/alpha/ecoff_machdep.h"
4656SN/A#include "base/loader/coff_sym.h"
4756SN/A#include "base/loader/coff_symconst.h"
487676Snate@binkert.org#include "base/loader/exec_ecoff.h"
4912SN/A
5012SN/Ausing namespace std;
5112SN/A
5212SN/AObjectFile *
5310880SCurtis.Dunham@arm.comEcoffObject::tryFile(const string &fname, size_t len, uint8_t *data)
5412SN/A{
5512SN/A    if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
5612SN/A        // it's Alpha ECOFF
5710880SCurtis.Dunham@arm.com        return new EcoffObject(fname, len, data,
58360SN/A                               ObjectFile::Alpha, ObjectFile::Tru64);
5912SN/A    }
6012SN/A    else {
6112SN/A        return NULL;
6212SN/A    }
6312SN/A}
6412SN/A
6512SN/A
6610880SCurtis.Dunham@arm.comEcoffObject::EcoffObject(const string &_filename, size_t _len, uint8_t *_data,
67360SN/A                         Arch _arch, OpSys _opSys)
6810880SCurtis.Dunham@arm.com    : ObjectFile(_filename, _len, _data, _arch, _opSys)
6912SN/A{
7012SN/A    execHdr = (ecoff_exechdr *)fileData;
7112SN/A    fileHdr = &(execHdr->f);
7212SN/A    aoutHdr = &(execHdr->a);
7312SN/A
7412SN/A    entry = aoutHdr->entry;
7512SN/A
7612SN/A    text.baseAddr = aoutHdr->text_start;
7712SN/A    text.size = aoutHdr->tsize;
782420SN/A    text.fileImage = fileData + ECOFF_TXTOFF(execHdr);
7912SN/A
8012SN/A    data.baseAddr = aoutHdr->data_start;
8112SN/A    data.size = aoutHdr->dsize;
822420SN/A    data.fileImage = fileData + ECOFF_DATOFF(execHdr);
8312SN/A
8412SN/A    bss.baseAddr = aoutHdr->bss_start;
8512SN/A    bss.size = aoutHdr->bsize;
862420SN/A    bss.fileImage = NULL;
8712SN/A
8812SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
8912SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
9012SN/A             bss.baseAddr, bss.size);
9112SN/A}
9212SN/A
9311392Sbrandon.potter@amd.combool
9411392Sbrandon.potter@amd.comEcoffObject::loadAllSymbols(SymbolTable *symtab, Addr base, Addr offset,
9511392Sbrandon.potter@amd.com                            Addr addr_mask)
9611392Sbrandon.potter@amd.com{
9711392Sbrandon.potter@amd.com    bool retval = loadGlobalSymbols(symtab, base, offset, addr_mask);
9811392Sbrandon.potter@amd.com    retval = retval && loadLocalSymbols(symtab, base, offset, addr_mask);
9911392Sbrandon.potter@amd.com    return retval;
10011392Sbrandon.potter@amd.com}
10112SN/A
10212SN/Abool
10311392Sbrandon.potter@amd.comEcoffObject::loadGlobalSymbols(SymbolTable *symtab, Addr base, Addr offset,
10411392Sbrandon.potter@amd.com                               Addr addr_mask)
10512SN/A{
10612SN/A    if (!symtab)
10712SN/A        return false;
10812SN/A
10912SN/A    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
1101252SN/A        warn("loadGlobalSymbols: wrong magic on %s\n", filename);
11112SN/A        return false;
11212SN/A    }
11312SN/A
11412SN/A    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
11512SN/A    if (syms->magic != magicSym2) {
1161252SN/A        warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
1171252SN/A        return false;
11812SN/A    }
11912SN/A
12012SN/A    ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset);
12112SN/A
12212SN/A    char *ext_strings = (char *)(fileData + syms->cbSsExtOffset);
12312SN/A    for (int i = 0; i < syms->iextMax; i++) {
12412SN/A        ecoff_sym *entry = &(ext_syms[i].asym);
12512SN/A        if (entry->iss != -1)
12612SN/A            symtab->insert(entry->value, ext_strings + entry->iss);
12712SN/A    }
12812SN/A
12912SN/A    return true;
13012SN/A}
13112SN/A
13212SN/Abool
13311392Sbrandon.potter@amd.comEcoffObject::loadLocalSymbols(SymbolTable *symtab, Addr base, Addr offset,
13411392Sbrandon.potter@amd.com                              Addr addr_mask)
13512SN/A{
13612SN/A    if (!symtab)
13712SN/A        return false;
13812SN/A
13912SN/A    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
1401252SN/A        warn("loadGlobalSymbols: wrong magic on %s\n", filename);
14112SN/A        return false;
14212SN/A    }
14312SN/A
14412SN/A    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
14512SN/A    if (syms->magic != magicSym2) {
1461252SN/A        warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
1471252SN/A        return false;
14812SN/A    }
14912SN/A
15012SN/A    ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset);
15112SN/A    char *local_strings = (char *)(fileData + syms->cbSsOffset);
15212SN/A    ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset);
15312SN/A
15412SN/A    for (int i = 0; i < syms->ifdMax; i++) {
15512SN/A        ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase);
15612SN/A        char *strings = (char *)(local_strings + fdesc[i].issBase);
15712SN/A        for (int j = 0; j < fdesc[i].csym; j++) {
15812SN/A            if (entry[j].st == stGlobal || entry[j].st == stProc)
15912SN/A                if (entry[j].iss != -1)
16012SN/A                    symtab->insert(entry[j].value, strings + entry[j].iss);
16112SN/A        }
16212SN/A    }
16312SN/A
16412SN/A    for (int i = 0; i < syms->isymMax; i++) {
16512SN/A        ecoff_sym *entry = &(local_syms[i]);
16612SN/A        if (entry->st == stProc)
16712SN/A            symtab->insert(entry->value, local_strings + entry->iss);
16812SN/A    }
16912SN/A
17012SN/A    return true;
17112SN/A}
172