ecoff_object.cc revision 8232
12817Sksewell@umich.edu/* 22817Sksewell@umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan 32817Sksewell@umich.edu * All rights reserved. 42817Sksewell@umich.edu * 52817Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without 62817Sksewell@umich.edu * modification, are permitted provided that the following conditions are 72817Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 82817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 92817Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 102817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 112817Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 122817Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 132817Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 142817Sksewell@umich.edu * this software without specific prior written permission. 152817Sksewell@umich.edu * 162817Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172817Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182817Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192817Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202817Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212817Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222817Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232817Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242817Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252817Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262817Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272817Sksewell@umich.edu * 282817Sksewell@umich.edu * Authors: Steve Reinhardt 292817Sksewell@umich.edu */ 302817Sksewell@umich.edu 312817Sksewell@umich.edu#include <string> 322817Sksewell@umich.edu 332817Sksewell@umich.edu#include "base/loader/ecoff_object.hh" 346658Snate@binkert.org#include "base/loader/symtab.hh" 358229Snate@binkert.org#include "base/misc.hh" 362935Sksewell@umich.edu#include "base/trace.hh" 372817Sksewell@umich.edu#include "base/types.hh" 382834Sksewell@umich.edu#include "debug/Loader.hh" 392834Sksewell@umich.edu 402834Sksewell@umich.edu// Only alpha will be able to load ecoff files for now. 412834Sksewell@umich.edu// base/types.hh and ecoff_machdep.h must be before the other .h files 422834Sksewell@umich.edu// because they are are gathered from other code bases and require some 432834Sksewell@umich.edu// typedefs from those files. 442834Sksewell@umich.edu#include "arch/alpha/ecoff_machdep.h" 452817Sksewell@umich.edu#include "base/loader/coff_sym.h" 462817Sksewell@umich.edu#include "base/loader/coff_symconst.h" 472817Sksewell@umich.edu#include "base/loader/exec_ecoff.h" 482817Sksewell@umich.edu 492817Sksewell@umich.eduusing namespace std; 502817Sksewell@umich.edu 512817Sksewell@umich.eduObjectFile * 522817Sksewell@umich.eduEcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 532817Sksewell@umich.edu{ 542817Sksewell@umich.edu if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) { 552817Sksewell@umich.edu // it's Alpha ECOFF 562817Sksewell@umich.edu return new EcoffObject(fname, fd, len, data, 572817Sksewell@umich.edu ObjectFile::Alpha, ObjectFile::Tru64); 582817Sksewell@umich.edu } 592817Sksewell@umich.edu else { 602817Sksewell@umich.edu return NULL; 612817Sksewell@umich.edu } 622817Sksewell@umich.edu} 632817Sksewell@umich.edu 642817Sksewell@umich.edu 652817Sksewell@umich.eduEcoffObject::EcoffObject(const string &_filename, int _fd, 662817Sksewell@umich.edu size_t _len, uint8_t *_data, 672817Sksewell@umich.edu Arch _arch, OpSys _opSys) 682817Sksewell@umich.edu : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 692817Sksewell@umich.edu{ 703784Sgblack@eecs.umich.edu execHdr = (ecoff_exechdr *)fileData; 716022Sgblack@eecs.umich.edu fileHdr = &(execHdr->f); 723784Sgblack@eecs.umich.edu aoutHdr = &(execHdr->a); 733784Sgblack@eecs.umich.edu 746022Sgblack@eecs.umich.edu entry = aoutHdr->entry; 753784Sgblack@eecs.umich.edu 768541Sgblack@eecs.umich.edu text.baseAddr = aoutHdr->text_start; 778541Sgblack@eecs.umich.edu text.size = aoutHdr->tsize; 782817Sksewell@umich.edu text.fileImage = fileData + ECOFF_TXTOFF(execHdr); 792817Sksewell@umich.edu 802817Sksewell@umich.edu data.baseAddr = aoutHdr->data_start; 812817Sksewell@umich.edu data.size = aoutHdr->dsize; 825712Shsul@eecs.umich.edu data.fileImage = fileData + ECOFF_DATOFF(execHdr); 832817Sksewell@umich.edu 845714Shsul@eecs.umich.edu bss.baseAddr = aoutHdr->bss_start; 855714Shsul@eecs.umich.edu bss.size = aoutHdr->bsize; 865714Shsul@eecs.umich.edu bss.fileImage = NULL; 875714Shsul@eecs.umich.edu 885715Shsul@eecs.umich.edu DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 895715Shsul@eecs.umich.edu text.baseAddr, text.size, data.baseAddr, data.size, 905715Shsul@eecs.umich.edu bss.baseAddr, bss.size); 915715Shsul@eecs.umich.edu} 922817Sksewell@umich.edu 932817Sksewell@umich.edu 942817Sksewell@umich.edubool 952817Sksewell@umich.eduEcoffObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 963548Sgblack@eecs.umich.edu{ 972817Sksewell@umich.edu if (!symtab) 982817Sksewell@umich.edu return false; 998541Sgblack@eecs.umich.edu 1008541Sgblack@eecs.umich.edu if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) { 1018754Sgblack@eecs.umich.edu warn("loadGlobalSymbols: wrong magic on %s\n", filename); 1028706Sandreas.hansson@arm.com return false; 1032817Sksewell@umich.edu } 1048706Sandreas.hansson@arm.com 1053675Sktlim@umich.edu ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr); 1068706Sandreas.hansson@arm.com if (syms->magic != magicSym2) { 1078706Sandreas.hansson@arm.com warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename); 1088799Sgblack@eecs.umich.edu return false; 1098706Sandreas.hansson@arm.com } 1108706Sandreas.hansson@arm.com 1112817Sksewell@umich.edu ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset); 1122817Sksewell@umich.edu 1132817Sksewell@umich.edu char *ext_strings = (char *)(fileData + syms->cbSsExtOffset); 1142817Sksewell@umich.edu for (int i = 0; i < syms->iextMax; i++) { 1152817Sksewell@umich.edu ecoff_sym *entry = &(ext_syms[i].asym); 1162817Sksewell@umich.edu if (entry->iss != -1) 1172817Sksewell@umich.edu symtab->insert(entry->value, ext_strings + entry->iss); 1182817Sksewell@umich.edu } 1192817Sksewell@umich.edu 1202817Sksewell@umich.edu return true; 1212817Sksewell@umich.edu} 1222817Sksewell@umich.edu 1232817Sksewell@umich.edubool 1245250Sksewell@umich.eduEcoffObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 1252817Sksewell@umich.edu{ 1262817Sksewell@umich.edu if (!symtab) 1275250Sksewell@umich.edu return false; 1282817Sksewell@umich.edu 1292817Sksewell@umich.edu if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) { 1302817Sksewell@umich.edu warn("loadGlobalSymbols: wrong magic on %s\n", filename); 1312817Sksewell@umich.edu return false; 1322817Sksewell@umich.edu } 1338777Sgblack@eecs.umich.edu 1342817Sksewell@umich.edu ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr); 1352817Sksewell@umich.edu if (syms->magic != magicSym2) { 1362817Sksewell@umich.edu warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename); 1372817Sksewell@umich.edu return false; 1382817Sksewell@umich.edu } 1392817Sksewell@umich.edu 1402817Sksewell@umich.edu ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset); 1412817Sksewell@umich.edu char *local_strings = (char *)(fileData + syms->cbSsOffset); 1422817Sksewell@umich.edu ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset); 1432817Sksewell@umich.edu 1442817Sksewell@umich.edu for (int i = 0; i < syms->ifdMax; i++) { 1452817Sksewell@umich.edu ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase); 1462817Sksewell@umich.edu char *strings = (char *)(local_strings + fdesc[i].issBase); 1472817Sksewell@umich.edu for (int j = 0; j < fdesc[i].csym; j++) { 1482817Sksewell@umich.edu if (entry[j].st == stGlobal || entry[j].st == stProc) 1492817Sksewell@umich.edu if (entry[j].iss != -1) 1502817Sksewell@umich.edu symtab->insert(entry[j].value, strings + entry[j].iss); 1512817Sksewell@umich.edu } 1522817Sksewell@umich.edu } 1532817Sksewell@umich.edu 1542817Sksewell@umich.edu for (int i = 0; i < syms->isymMax; i++) { 1552817Sksewell@umich.edu ecoff_sym *entry = &(local_syms[i]); 1562817Sksewell@umich.edu if (entry->st == stProc) 1572817Sksewell@umich.edu symtab->insert(entry->value, local_strings + entry->iss); 1582817Sksewell@umich.edu } 1592817Sksewell@umich.edu 1602817Sksewell@umich.edu return true; 1612817Sksewell@umich.edu} 1622817Sksewell@umich.edu