object_file.hh revision 11906
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 2002-2004 The Regents of The University of Michigan
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org *
286145Snate@binkert.org * Authors: Nathan Binkert
296154Snate@binkert.org *          Steve Reinhardt
306154Snate@binkert.org */
316145Snate@binkert.org
326145Snate@binkert.org#ifndef __OBJECT_FILE_HH__
336145Snate@binkert.org#define __OBJECT_FILE_HH__
346145Snate@binkert.org
356145Snate@binkert.org#include <limits>
366145Snate@binkert.org#include <string>
377039Snate@binkert.org
387039Snate@binkert.org#include "base/misc.hh"
396145Snate@binkert.org#include "base/types.hh"
407039Snate@binkert.org
416145Snate@binkert.orgclass PortProxy;
426145Snate@binkert.orgclass SymbolTable;
437039Snate@binkert.org
447039Snate@binkert.orgclass ObjectFile
456145Snate@binkert.org{
467039Snate@binkert.org  public:
477039Snate@binkert.org
487039Snate@binkert.org    enum Arch {
497039Snate@binkert.org        UnknownArch,
506145Snate@binkert.org        Alpha,
516145Snate@binkert.org        SPARC64,
527039Snate@binkert.org        SPARC32,
537039Snate@binkert.org        Mips,
546145Snate@binkert.org        X86_64,
557039Snate@binkert.org        I386,
567039Snate@binkert.org        Arm64,
576145Snate@binkert.org        Arm,
586145Snate@binkert.org        Thumb,
597039Snate@binkert.org        Power,
607039Snate@binkert.org        Riscv
616145Snate@binkert.org    };
627039Snate@binkert.org
637039Snate@binkert.org    enum OpSys {
647039Snate@binkert.org        UnknownOpSys,
657039Snate@binkert.org        Tru64,
666145Snate@binkert.org        Linux,
676145Snate@binkert.org        Solaris,
687039Snate@binkert.org        LinuxArmOABI,
697039Snate@binkert.org        FreeBSD
706145Snate@binkert.org    };
717039Snate@binkert.org
726145Snate@binkert.org  protected:
736145Snate@binkert.org    const std::string filename;
747039Snate@binkert.org    uint8_t *fileData;
757039Snate@binkert.org    size_t len;
766145Snate@binkert.org
777039Snate@binkert.org    Arch arch;
787039Snate@binkert.org    OpSys opSys;
797039Snate@binkert.org
807039Snate@binkert.org    ObjectFile(const std::string &_filename, size_t _len, uint8_t *_data,
816145Snate@binkert.org               Arch _arch, OpSys _opSys);
826145Snate@binkert.org
837039Snate@binkert.org  public:
847039Snate@binkert.org    virtual ~ObjectFile();
856145Snate@binkert.org
867039Snate@binkert.org    static const Addr maxAddr = std::numeric_limits<Addr>::max();
877039Snate@binkert.org
887039Snate@binkert.org    virtual bool loadSections(PortProxy& mem_proxy,
896145Snate@binkert.org                              Addr mask = maxAddr, Addr offset = 0);
906145Snate@binkert.org
917039Snate@binkert.org    virtual bool loadAllSymbols(SymbolTable *symtab, Addr base = 0,
927039Snate@binkert.org                                Addr offset = 0, Addr mask = maxAddr) = 0;
936145Snate@binkert.org    virtual bool loadGlobalSymbols(SymbolTable *symtab, Addr base = 0,
947039Snate@binkert.org                                   Addr offset = 0, Addr mask = maxAddr) = 0;
957039Snate@binkert.org    virtual bool loadLocalSymbols(SymbolTable *symtab, Addr base = 0,
967039Snate@binkert.org                                  Addr offset = 0, Addr mask = maxAddr) = 0;
977039Snate@binkert.org    virtual bool loadWeakSymbols(SymbolTable *symtab, Addr base = 0,
986145Snate@binkert.org                                 Addr offset = 0, Addr mask = maxAddr)
996145Snate@binkert.org    { return false; }
1007039Snate@binkert.org
1017039Snate@binkert.org    virtual ObjectFile *getInterpreter() const { return nullptr; }
1027039Snate@binkert.org    virtual bool relocatable() const { return false; }
1037039Snate@binkert.org    virtual Addr mapSize() const
1047039Snate@binkert.org    { panic("mapSize() should only be called on relocatable objects\n"); }
1057039Snate@binkert.org    virtual void updateBias(Addr bias_addr)
1067039Snate@binkert.org    { panic("updateBias() should only be called on relocatable objects\n"); }
1076145Snate@binkert.org    virtual Addr bias() const { return 0; }
1086145Snate@binkert.org
1096145Snate@binkert.org    virtual bool hasTLS() { return false; }
1107039Snate@binkert.org
1117039Snate@binkert.org    Arch  getArch()  const { return arch; }
1127039Snate@binkert.org    OpSys getOpSys() const { return opSys; }
1137039Snate@binkert.org
1147039Snate@binkert.org  protected:
1157039Snate@binkert.org
1167039Snate@binkert.org    struct Section {
1177039Snate@binkert.org        Addr baseAddr;
1187039Snate@binkert.org        uint8_t *fileImage;
1197039Snate@binkert.org        size_t size;
1207039Snate@binkert.org    };
1216145Snate@binkert.org
1227039Snate@binkert.org    Addr entry;
1237039Snate@binkert.org    Addr globalPtr;
1246145Snate@binkert.org
1256145Snate@binkert.org    Section text;
1267039Snate@binkert.org    Section data;
1277039Snate@binkert.org    Section bss;
1286145Snate@binkert.org
1297039Snate@binkert.org    bool loadSection(Section *sec, PortProxy& mem_proxy, Addr mask,
1307039Snate@binkert.org                     Addr offset = 0);
1317039Snate@binkert.org    void setGlobalPointer(Addr global_ptr) { globalPtr = global_ptr; }
1327039Snate@binkert.org
1337039Snate@binkert.org  public:
1346145Snate@binkert.org    Addr entryPoint() const { return entry; }
1356145Snate@binkert.org
1367039Snate@binkert.org    Addr globalPointer() const { return globalPtr; }
1377039Snate@binkert.org
1387039Snate@binkert.org    Addr textBase() const { return text.baseAddr; }
1397039Snate@binkert.org    Addr dataBase() const { return data.baseAddr; }
1406145Snate@binkert.org    Addr bssBase() const { return bss.baseAddr; }
1416145Snate@binkert.org
1427039Snate@binkert.org    size_t textSize() const { return text.size; }
1437039Snate@binkert.org    size_t dataSize() const { return data.size; }
1446145Snate@binkert.org    size_t bssSize() const { return bss.size; }
1457039Snate@binkert.org
1467039Snate@binkert.org    /* This function allows you to override the base address where
1477039Snate@binkert.org     * a binary is going to be loaded or set it if the binary is just a
1487039Snate@binkert.org     * blob that doesn't include an object header.
1497039Snate@binkert.org     * @param a address to load the binary/text section at
1507039Snate@binkert.org     */
1517039Snate@binkert.org    void setTextBase(Addr a) { text.baseAddr = a; }
1527039Snate@binkert.org};
1536145Snate@binkert.org
1547039Snate@binkert.orgObjectFile *createObjectFile(const std::string &fname, bool raw = false);
1556145Snate@binkert.org
1566145Snate@binkert.org
1577039Snate@binkert.org#endif // __OBJECT_FILE_HH__
1587039Snate@binkert.org