12SN/A/*
21762SN/A * Copyright (c) 2002-2004 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302SN/A */
312SN/A
322SN/A#ifndef __OBJECT_FILE_HH__
332SN/A#define __OBJECT_FILE_HH__
342SN/A
352520SN/A#include <limits>
362207SN/A#include <string>
372207SN/A
3812334Sgabeblack@google.com#include "base/logging.hh"
396214Snate@binkert.org#include "base/types.hh"
402SN/A
418706Sandreas.hansson@arm.comclass PortProxy;
4213982Sgabeblack@google.comclass Process;
4313982Sgabeblack@google.comclass ProcessParams;
442SN/Aclass SymbolTable;
452SN/A
462SN/Aclass ObjectFile
472SN/A{
48360SN/A  public:
49360SN/A
50360SN/A    enum Arch {
51360SN/A        UnknownArch,
522207SN/A        Alpha,
534111Sgblack@eecs.umich.edu        SPARC64,
544111Sgblack@eecs.umich.edu        SPARC32,
554155Sgblack@eecs.umich.edu        Mips,
565874Sgblack@eecs.umich.edu        X86_64,
575874Sgblack@eecs.umich.edu        I386,
5810037SARM gem5 Developers        Arm64,
596691Stjones1@inf.ed.ac.uk        Arm,
607095Sgblack@eecs.umich.edu        Thumb,
6111723Sar4jc@virginia.edu        Power,
6213634Saustinharris@utexas.edu        Riscv64,
6313634Saustinharris@utexas.edu        Riscv32
64360SN/A    };
65360SN/A
66360SN/A    enum OpSys {
67360SN/A        UnknownOpSys,
68360SN/A        Tru64,
692207SN/A        Linux,
706392Ssaidi@eecs.umich.edu        Solaris,
7110810Sbr@bsdpad.com        LinuxArmOABI,
7210810Sbr@bsdpad.com        FreeBSD
73360SN/A    };
74360SN/A
752SN/A  protected:
7612SN/A    const std::string filename;
7712SN/A    uint8_t *fileData;
782SN/A    size_t len;
792SN/A
8011906SBrandon.Potter@amd.com    Arch arch;
81360SN/A    OpSys opSys;
82360SN/A
8310880SCurtis.Dunham@arm.com    ObjectFile(const std::string &_filename, size_t _len, uint8_t *_data,
84360SN/A               Arch _arch, OpSys _opSys);
8512SN/A
862SN/A  public:
872SN/A    virtual ~ObjectFile();
882SN/A
8911392Sbrandon.potter@amd.com    static const Addr maxAddr = std::numeric_limits<Addr>::max();
9011392Sbrandon.potter@amd.com
9114017Sbrandon.potter@amd.com    virtual bool loadSections(const PortProxy& mem_proxy,
9211392Sbrandon.potter@amd.com                              Addr mask = maxAddr, Addr offset = 0);
9311392Sbrandon.potter@amd.com
9411392Sbrandon.potter@amd.com    virtual bool loadAllSymbols(SymbolTable *symtab, Addr base = 0,
9511392Sbrandon.potter@amd.com                                Addr offset = 0, Addr mask = maxAddr) = 0;
9611392Sbrandon.potter@amd.com    virtual bool loadGlobalSymbols(SymbolTable *symtab, Addr base = 0,
9711392Sbrandon.potter@amd.com                                   Addr offset = 0, Addr mask = maxAddr) = 0;
9811392Sbrandon.potter@amd.com    virtual bool loadLocalSymbols(SymbolTable *symtab, Addr base = 0,
9911392Sbrandon.potter@amd.com                                  Addr offset = 0, Addr mask = maxAddr) = 0;
10011392Sbrandon.potter@amd.com    virtual bool loadWeakSymbols(SymbolTable *symtab, Addr base = 0,
10111392Sbrandon.potter@amd.com                                 Addr offset = 0, Addr mask = maxAddr)
1029641Sguodeyuan@tsinghua.org.cn    { return false; }
1032SN/A
10411389Sbrandon.potter@amd.com    virtual ObjectFile *getInterpreter() const { return nullptr; }
10511389Sbrandon.potter@amd.com    virtual bool relocatable() const { return false; }
10611389Sbrandon.potter@amd.com    virtual Addr mapSize() const
10711389Sbrandon.potter@amd.com    { panic("mapSize() should only be called on relocatable objects\n"); }
10811389Sbrandon.potter@amd.com    virtual void updateBias(Addr bias_addr)
10911389Sbrandon.potter@amd.com    { panic("updateBias() should only be called on relocatable objects\n"); }
11011389Sbrandon.potter@amd.com    virtual Addr bias() const { return 0; }
11111389Sbrandon.potter@amd.com
1125070Ssaidi@eecs.umich.edu    virtual bool hasTLS() { return false; }
1133917Ssaidi@eecs.umich.edu
114360SN/A    Arch  getArch()  const { return arch; }
115360SN/A    OpSys getOpSys() const { return opSys; }
116360SN/A
1172SN/A  protected:
1182SN/A
11912SN/A    struct Section {
12011906SBrandon.Potter@amd.com        Addr baseAddr;
1212420SN/A        uint8_t *fileImage;
12211906SBrandon.Potter@amd.com        size_t size;
12312SN/A    };
12412SN/A
12512SN/A    Addr entry;
12612SN/A    Addr globalPtr;
12712SN/A
12812SN/A    Section text;
12912SN/A    Section data;
13012SN/A    Section bss;
1312SN/A
13214017Sbrandon.potter@amd.com    bool loadSection(Section *sec, const PortProxy& mem_proxy, Addr mask,
13310037SARM gem5 Developers                     Addr offset = 0);
1342472SN/A    void setGlobalPointer(Addr global_ptr) { globalPtr = global_ptr; }
1352420SN/A
1362SN/A  public:
13712SN/A    Addr entryPoint() const { return entry; }
1382472SN/A
13912SN/A    Addr globalPointer() const { return globalPtr; }
1402SN/A
14112SN/A    Addr textBase() const { return text.baseAddr; }
14212SN/A    Addr dataBase() const { return data.baseAddr; }
14312SN/A    Addr bssBase() const { return bss.baseAddr; }
14412SN/A
14512SN/A    size_t textSize() const { return text.size; }
14612SN/A    size_t dataSize() const { return data.size; }
14712SN/A    size_t bssSize() const { return bss.size; }
1483584Ssaidi@eecs.umich.edu
1499261Sdam.sunwoo@arm.com    /* This function allows you to override the base address where
1509261Sdam.sunwoo@arm.com     * a binary is going to be loaded or set it if the binary is just a
1519261Sdam.sunwoo@arm.com     * blob that doesn't include an object header.
1529261Sdam.sunwoo@arm.com     * @param a address to load the binary/text section at
1539261Sdam.sunwoo@arm.com     */
1543584Ssaidi@eecs.umich.edu    void setTextBase(Addr a) { text.baseAddr = a; }
15513982Sgabeblack@google.com
15613982Sgabeblack@google.com    /**
15713982Sgabeblack@google.com     * Each instance of a Loader subclass will have a chance to try to load
15813982Sgabeblack@google.com     * an object file when tryLoaders is called. If they can't because they
15913982Sgabeblack@google.com     * aren't compatible with it (wrong arch, wrong OS, etc), then they
16013982Sgabeblack@google.com     * silently fail by returning nullptr so other loaders can try.
16113982Sgabeblack@google.com     */
16213982Sgabeblack@google.com    class Loader
16313982Sgabeblack@google.com    {
16413982Sgabeblack@google.com      public:
16513982Sgabeblack@google.com        Loader();
16613982Sgabeblack@google.com
16713982Sgabeblack@google.com        /* Loader instances are singletons. */
16813982Sgabeblack@google.com        Loader(const Loader &) = delete;
16913982Sgabeblack@google.com        void operator=(const Loader &) = delete;
17013982Sgabeblack@google.com
17113982Sgabeblack@google.com        virtual ~Loader() {}
17213982Sgabeblack@google.com
17313982Sgabeblack@google.com        /**
17413982Sgabeblack@google.com         * Each subclass needs to implement this method. If the loader is
17513982Sgabeblack@google.com         * compatible with the passed in object file, it should return the
17613982Sgabeblack@google.com         * created Process object corresponding to it. If not, it should fail
17713982Sgabeblack@google.com         * silently and return nullptr. If there's a non-compatibliity related
17813982Sgabeblack@google.com         * error like file IO errors, etc., those should fail non-silently
17913982Sgabeblack@google.com         * with a panic or fail as normal.
18013982Sgabeblack@google.com         */
18113982Sgabeblack@google.com        virtual Process *load(ProcessParams *params, ObjectFile *obj_file) = 0;
18213982Sgabeblack@google.com    };
18313982Sgabeblack@google.com
18413982Sgabeblack@google.com    // Try all the Loader instance's "load" methods one by one until one is
18513982Sgabeblack@google.com    // successful. If none are, complain and fail.
18613982Sgabeblack@google.com    static Process *tryLoaders(ProcessParams *params, ObjectFile *obj_file);
1872SN/A};
1882SN/A
1893584Ssaidi@eecs.umich.eduObjectFile *createObjectFile(const std::string &fname, bool raw = false);
1902SN/A
1912SN/A
1922SN/A#endif // __OBJECT_FILE_HH__
193