test,base: Added GTest for base/loader/image_file_data.cc
[gem5.git] / src / base / loader / object_file.hh
1 /*
2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
32 #ifndef __BASE_LOADER_OBJECT_FILE_HH__
33 #define __BASE_LOADER_OBJECT_FILE_HH__
34
35 #include <string>
36
37 #include "base/loader/image_file.hh"
38 #include "base/loader/image_file_data.hh"
39 #include "base/loader/memory_image.hh"
40 #include "base/logging.hh"
41 #include "base/types.hh"
42
43 class SymbolTable;
44
45 class ObjectFile : public ImageFile
46 {
47 public:
48
49 enum Arch {
50 UnknownArch,
51 Alpha,
52 SPARC64,
53 SPARC32,
54 Mips,
55 X86_64,
56 I386,
57 Arm64,
58 Arm,
59 Thumb,
60 Power,
61 Riscv64,
62 Riscv32
63 };
64
65 enum OpSys {
66 UnknownOpSys,
67 Tru64,
68 Linux,
69 Solaris,
70 LinuxArmOABI,
71 FreeBSD
72 };
73
74 protected:
75 Arch arch = UnknownArch;
76 OpSys opSys = UnknownOpSys;
77
78 ObjectFile(ImageFileDataPtr ifd);
79
80 public:
81 virtual ~ObjectFile() {};
82
83 virtual bool
84 loadAllSymbols(SymbolTable *symtab, Addr base=0,
85 Addr offset=0, Addr mask=MaxAddr)
86 {
87 return true;
88 };
89 virtual bool
90 loadGlobalSymbols(SymbolTable *symtab, Addr base=0,
91 Addr offset=0, Addr mask=MaxAddr)
92 {
93 return true;
94 }
95 virtual bool
96 loadLocalSymbols(SymbolTable *symtab, Addr base=0,
97 Addr offset=0, Addr mask=MaxAddr)
98 {
99 return true;
100 }
101 virtual bool
102 loadWeakSymbols(SymbolTable *symtab, Addr base=0,
103 Addr offset=0, Addr mask=MaxAddr)
104 {
105 return true;
106 }
107
108 virtual ObjectFile *getInterpreter() const { return nullptr; }
109 virtual bool relocatable() const { return false; }
110 virtual Addr
111 mapSize() const
112 {
113 panic("mapSize() should only be called on relocatable objects\n");
114 }
115 virtual void
116 updateBias(Addr bias_addr)
117 {
118 panic("updateBias() should only be called on relocatable objects\n");
119 }
120 virtual Addr bias() const { return 0; }
121
122 virtual bool hasTLS() { return false; }
123
124 Arch getArch() const { return arch; }
125 OpSys getOpSys() const { return opSys; }
126
127 protected:
128 Addr entry = 0;
129
130 public:
131 Addr entryPoint() const { return entry; }
132 };
133
134 class ObjectFileFormat
135 {
136 protected:
137 ObjectFileFormat();
138
139 public:
140 ObjectFileFormat(const ObjectFileFormat &) = delete;
141 void operator=(const ObjectFileFormat &) = delete;
142
143 virtual ObjectFile *load(ImageFileDataPtr data) = 0;
144 };
145
146 ObjectFile *createObjectFile(const std::string &fname, bool raw=false);
147
148 #endif // __BASE_LOADER_OBJECT_FILE_HH__