1 /* Copyright (C) 2021 Free Software Foundation, Inc.
2    Contributed by Oracle.
3 
4    This file is part of GNU Binutils.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #ifndef _MODULE_H
22 #define _MODULE_H
23 
24 // A Module object represents a .o file that was used to build up a segement.
25 //	Its main function is to compute source and disassembly annotations
26 // Text reordering and/or function outlining imply that a module may
27 //	not be contiguous.
28 
29 #include "Histable.h"
30 #include "Hist_data.h"
31 
32 #define MOD_FLAG_UNKNOWN 0x01
33 
34 class LoadObject;
35 class MetricList;
36 class ComC;
37 class Disasm;
38 class Hist_data;
39 class Stabs;
40 class SourceFile;
41 class DataObject;
42 class JMethod;
43 template <class ITEM> class Vector;
44 
45 class InlinedSubr
46 {
47 public:
48   InlinedSubr ();
49   DbeLine *dbeLine;
50   Function *func;
51   char *fname;
52   uint64_t low_pc;
53   uint64_t high_pc;
54   int level;
55 
56   bool
contains(InlinedSubr * p)57   contains (InlinedSubr *p)
58   {
59     return low_pc <= p->low_pc && high_pc >= p->high_pc;
60   }
61 
62   bool
contains(uint64_t pc)63   contains (uint64_t pc)
64   {
65     return low_pc <= pc && high_pc > pc;
66   }
67 };
68 
69 class Module : public HistableFile
70 {
71 public:
72   // Annotated Source or Disassembly
73   enum Anno_Errors
74   {
75     AE_OK,
76     AE_NOTREAD,
77     AE_NOSRC,
78     AE_NOOBJ,
79     AE_NOLOBJ,
80     AE_NOSTABS,
81     AE_NOSYMTAB,
82     AE_TIMESRC,
83     AE_TIMEDIS,
84     AE_TIMESTABS,
85     AE_TIMESTABS_DIFF,
86     AE_OTHER
87   };
88 
89   // The following enums are duplicated in Java
90   enum Anno_Types
91   {
92     AT_LIST = 0,
93     AT_SRC,
94     AT_SRC_ONLY,
95     AT_DIS,
96     AT_COM,
97     AT_QUOTE,
98     AT_FUNC,
99     AT_EMPTY,
100     AT_DIS_ONLY
101   };
102 
103   Module ();
104   virtual ~Module ();
105   virtual int64_t get_size ();
106   virtual void set_name (char *str);
107   virtual Vector<Histable*> *get_comparable_objs ();
108   virtual int readFile ();
109 
110   virtual Histable_type
get_type()111   get_type ()
112   {
113     return MODULE;
114   }
115 
116   inline Anno_Errors
get_status()117   get_status ()
118   {
119     return status;
120   }
121 
122   inline void
set_file_name(char * fnm)123   set_file_name (char *fnm)
124   {
125     free (file_name);
126     file_name = fnm;
127   }
128 
129   // get error string
130   char *anno_str (char *fnm = NULL);
131 
132   // generate annotated source/disassembly data
133   Hist_data *get_data (DbeView *dbev, MetricList *mlist,
134 		       Histable::Type type, TValue *ftotal, SourceFile *srcFile,
135 		       Function *func, Vector<int> *marks, int threshold,
136 		       int vis_bits, int src_visible, bool hex_visible,
137 		       bool func_scope, bool src_only,
138 		       Vector<int_pair_t> *marks2d = NULL,
139 		       Vector<int_pair_t> *marks2d_inc = NULL);
140 
141   Vector<uint64_t> *getAddrs (Function *func);
142   SourceFile *setIncludeFile (char *includeFile);
143 
144   SourceFile *
getIncludeFile()145   getIncludeFile ()
146   {
147     return curr_inc;
148   }
149 
150   SourceFile *
getMainSrc()151   getMainSrc ()
152   {
153     return main_source;
154   }
155 
156   char *
getResolvedObjectPath()157   getResolvedObjectPath ()
158   {
159     return stabsPath ? stabsPath : get_name ();
160   }
161 
162   char *
getDebugPath()163   getDebugPath ()
164   {
165     setFile ();
166     return stabsPath;
167   }
168 
169   void read_stabs (bool all = true);
170   void dump_dataobjects (FILE *out);
171   DataObject *get_dobj (uint32_t dtype_id);
172   void reset_datatypes ();
173   void read_hwcprof_info ();
174   bool is_fortran ();
175   SourceFile *findSource (const char *fname, bool create);
176   bool openStabs (bool all = true);
177   LoadObject *createLoadObject (const char *lo_name);
178   JMethod *find_jmethod (const char *nm, const char *sig);
179 
180   unsigned int flags;               // flags used for marking traversals
181   Sp_lang_code lang_code;           // What is source lang. in module
182   char *file_name;                  // Full path to actual source file
183   Vector<Function*> *functions;     // Unordered list of functions
184   LoadObject *loadobject;           // Parent loadobject
185   LoadObject *dot_o_file;           // The .o file with debug information
186   unsigned fragmented;              // -xF used when compiling module
187   int real_timestamp;               // Linked timestamp from N_OPT stab
188   int curr_timestamp;               // Current object timestamp from N_OPT stab
189   char *comp_flags;                 // compiler flags used to compile module
190   char *comp_dir;                   // directory used to compile module
191   char *linkerStabName;             // Name from 'N_UNDF' stab
192   Stabs *objStabs;                  // stabs of object file
193   bool readStabs;
194   bool hasStabs;
195   bool hasDwarf;
196   uint64_t hdrOffset;               // offset in .debug_info
197   unsigned hwcprof;                 // hwcprof info status
198   Vector<inst_info_t*> *infoList;   // merged list
199   Vector<memop_info_t*> ldMemops;   // load instructions
200   Vector<memop_info_t*> stMemops;   // store instructions
201   Vector<memop_info_t*> pfMemops;   // prefetch instructions
202   Vector<target_info_t*> bTargets;  // branch targets
203   Vector<datatype_t*> *datatypes;   // object type descriptors
204   Vector<SourceFile*> *includes;
205   Module *indexStabsLink;           // correspondent module for the .o file
206   InlinedSubr *inlinedSubr;
207 
208 protected:
209   void removeStabsTmp (); // Remove temporary *.o (got from *.a)
210 
211   // Check timestamp, warn users if src/dis/stabs later than exp.
212   Anno_Errors checkTimeStamp (bool chkDis);
213 
214   // Set paths for reading Stabs and Symbols
215   bool read_ar (int ar, int obj, char *obj_base);
216   bool setFile ();
217 
218   // Open appropriate symbol tables, construct set of PC ranges,
219   //	and maps to source lines for each PC
220   Stabs *openDebugInfo ();
221 
222   // Construct PC index table
223   bool openDisPC ();
224 
225   // Compute data--scan data to compute metrics per-src-line/dis-line
226   bool computeMetrics (DbeView *dbev, Function *func, MetricList *mlist,
227 		       Histable::Type type, bool src_metric,
228 		       bool func_scope, SourceFile *source);
229   void init_line ();
230   void init_index (Hist_data *witems, int &wlindex, int &wmsize, int &wmindex);
231 
232   void set_src_data (Function *func, int vis_bits, int cmpline_visible,
233 		     int funcline_visible);
234   void set_dis_data (Function *func, int vis_bits, int cmpline_visible,
235 		     int src_visible, bool hex_vis, bool func_scope,
236 		     int funcline_visible);
237   void set_src (Anno_Types type, DbeLine *dbeline);
238   void set_dis (DbeInstr *instr, Anno_Types type, bool nextFile, char *dis_str);
239   void set_MPSlave ();
240   void set_one (Hist_data::HistItem *org_item, Anno_Types type, const char *text);
241   void set_ComCom (int vis_bits);
242 
243   virtual char *get_disasm (uint64_t inst_address, uint64_t end_address,
244 			    uint64_t start_address, uint64_t f_offset,
245 			    int64_t &inst_size);
246 
247   Anno_Errors status;
248   Anno_Errors openSourceFlag;
249   bool hexVisible;          // show hex code in disasm
250   time_t disMTime;          // Creating time for disassembly
251   time_t stabsMTime;        // Creating time for stabs
252   SourceFile *main_source;
253   SourceFile *curr_inc;     // pointer to include file or NULL
254   SourceFile *srcContext;
255   Vector<ComC*> *comComs;   // table of compiler comments
256   Disasm *disasm;
257   Hist_data *src_items;
258   Hist_data *dis_items;
259   Hist_data *data_items;
260   DbeView * cur_dbev;
261   TValue *total;
262   TValue *maximum;
263   TValue *maximum_inc;
264   TValue *empty;
265   int name_idx;         // index of name metric in list for src/dis
266   int size_index;       // index of size metric in list for src/dis
267   int addr_index;       // index of address metric in list for src/dis
268 
269   int curline;          // line# of next source line to be processed
270   int cindex, cline;    // index and src line of next compiler-comment
271   int sindex, sline;    // index and src line of next item in src_items
272   int dindex;
273   DbeInstr *daddr;      // pointer to next DbeInstr with metrics
274   int mindex;           // MP index and src line of next metric-value
275   int mline;            // MP line to be processed by source
276 
277   char *disPath;        // path for disassembly
278   char *stabsPath;      // path for reading stabs
279   char *stabsTmp;       // temporary *.o from *.a
280   char *disName;        // library/path for disassembly
281   char *stabsName;      // library/path for stabs
282 };
283 
284 #endif /* _MODULE_H */
285