object_file.hh revision 11392
11689SN/A/*
22329SN/A * Copyright (c) 2002-2004 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
291689SN/A *          Steve Reinhardt
301689SN/A */
311717SN/A
321060SN/A#ifndef __OBJECT_FILE_HH__
332292SN/A#define __OBJECT_FILE_HH__
342292SN/A
351060SN/A#include <limits>
362292SN/A#include <string>
372292SN/A
382292SN/A#include "base/misc.hh"
392292SN/A#include "base/types.hh"
402292SN/A
412292SN/Aclass PortProxy;
422292SN/Aclass SymbolTable;
431060SN/A
442292SN/Aclass ObjectFile
452292SN/A{
462348SN/A  public:
472292SN/A
482292SN/A    enum Arch {
492292SN/A        UnknownArch,
502292SN/A        Alpha,
512292SN/A        SPARC64,
522292SN/A        SPARC32,
532292SN/A        Mips,
542292SN/A        X86_64,
552292SN/A        I386,
562292SN/A        Arm64,
572292SN/A        Arm,
582292SN/A        Thumb,
592292SN/A        Power
602292SN/A    };
612292SN/A
622292SN/A    enum OpSys {
632292SN/A        UnknownOpSys,
641060SN/A        Tru64,
651060SN/A        Linux,
661062SN/A        Solaris,
671062SN/A        LinuxArmOABI,
682292SN/A        FreeBSD
691062SN/A    };
701062SN/A
712307SN/A  protected:
721062SN/A    const std::string filename;
731062SN/A    uint8_t *fileData;
741062SN/A    size_t len;
752307SN/A
761062SN/A    Arch  arch;
771062SN/A    OpSys opSys;
782292SN/A
792307SN/A    ObjectFile(const std::string &_filename, size_t _len, uint8_t *_data,
802292SN/A               Arch _arch, OpSys _opSys);
812292SN/A
821062SN/A  public:
832307SN/A    virtual ~ObjectFile();
841062SN/A
851062SN/A    static const Addr maxAddr = std::numeric_limits<Addr>::max();
861062SN/A
872307SN/A    virtual bool loadSections(PortProxy& mem_proxy,
881062SN/A                              Addr mask = maxAddr, Addr offset = 0);
891062SN/A
902307SN/A    virtual bool loadAllSymbols(SymbolTable *symtab, Addr base = 0,
912307SN/A                                Addr offset = 0, Addr mask = maxAddr) = 0;
922307SN/A    virtual bool loadGlobalSymbols(SymbolTable *symtab, Addr base = 0,
932307SN/A                                   Addr offset = 0, Addr mask = maxAddr) = 0;
941062SN/A    virtual bool loadLocalSymbols(SymbolTable *symtab, Addr base = 0,
952307SN/A                                  Addr offset = 0, Addr mask = maxAddr) = 0;
961062SN/A    virtual bool loadWeakSymbols(SymbolTable *symtab, Addr base = 0,
971062SN/A                                 Addr offset = 0, Addr mask = maxAddr)
981062SN/A    { return false; }
992307SN/A
1001062SN/A    virtual ObjectFile *getInterpreter() const { return nullptr; }
1011062SN/A    virtual bool relocatable() const { return false; }
1021062SN/A    virtual Addr mapSize() const
1031062SN/A    { panic("mapSize() should only be called on relocatable objects\n"); }
1042307SN/A    virtual void updateBias(Addr bias_addr)
1051062SN/A    { panic("updateBias() should only be called on relocatable objects\n"); }
1061062SN/A    virtual Addr bias() const { return 0; }
1071062SN/A
1082307SN/A    virtual bool hasTLS() { return false; }
1091062SN/A
1101062SN/A    Arch  getArch()  const { return arch; }
1111062SN/A    OpSys getOpSys() const { return opSys; }
1121062SN/A
1131060SN/A  protected:
1141060SN/A
1152733Sktlim@umich.edu    struct Section {
1161060SN/A        Addr     baseAddr;
1172292SN/A        uint8_t *fileImage;
1181060SN/A        size_t   size;
1191060SN/A    };
1201060SN/A
1211060SN/A    Addr entry;
1221060SN/A    Addr globalPtr;
1232292SN/A
1241060SN/A    Section text;
1252292SN/A    Section data;
1261060SN/A    Section bss;
1271060SN/A
1281060SN/A    bool loadSection(Section *sec, PortProxy& mem_proxy, Addr mask,
1291060SN/A                     Addr offset = 0);
1301060SN/A    void setGlobalPointer(Addr global_ptr) { globalPtr = global_ptr; }
1311060SN/A
1321060SN/A  public:
1331060SN/A    Addr entryPoint() const { return entry; }
1341060SN/A
1351060SN/A    Addr globalPointer() const { return globalPtr; }
1361060SN/A
1371060SN/A    Addr textBase() const { return text.baseAddr; }
1381060SN/A    Addr dataBase() const { return data.baseAddr; }
1392292SN/A    Addr bssBase() const { return bss.baseAddr; }
1401060SN/A
1412292SN/A    size_t textSize() const { return text.size; }
1421060SN/A    size_t dataSize() const { return data.size; }
1431060SN/A    size_t bssSize() const { return bss.size; }
1441060SN/A
1451060SN/A    /* This function allows you to override the base address where
1461060SN/A     * a binary is going to be loaded or set it if the binary is just a
1471060SN/A     * blob that doesn't include an object header.
1481060SN/A     * @param a address to load the binary/text section at
1491060SN/A     */
1502292SN/A    void setTextBase(Addr a) { text.baseAddr = a; }
1511060SN/A};
1522292SN/A
1531060SN/AObjectFile *createObjectFile(const std::string &fname, bool raw = false);
1541060SN/A
1551060SN/A
1561060SN/A#endif // __OBJECT_FILE_HH__
1571060SN/A