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 _STABS_H
22 #define _STABS_H
23 
24 #include "dbe_structs.h"
25 #include "vec.h"
26 
27 enum cpf_instr_type_t {
28     CPF_INSTR_TYPE_LD = 0,              // profiled load instruction
29     CPF_INSTR_TYPE_ST,                  // profiled store instruction
30     CPF_INSTR_TYPE_PREFETCH,            // profiled prefetch instruction
31     CPF_INSTR_TYPE_BRTARGET,            // branch target
32     CPF_INSTR_TYPE_UNKNOWN,             // unidentified instruction
33     CPF_INSTR_TYPE_NTYPES               // total # of instr types
34 };
35 
36 class Function;
37 class LoadObject;
38 class Module;
39 class ComC;
40 class Elf;
41 class Dwarf;
42 class Symbol;
43 class Reloc;
44 struct cpf_stabs_t;
45 class SourceFile;
46 template <typename Key_t, typename Value_t> class Map;
47 
48 class Include {
49   public:
50     typedef struct {
51 	SourceFile  *srcfile;
52 	int         lineno;
53     } SrcFileInfo;
54     Include();
55     ~Include();
56     void    new_src_file(SourceFile *source, int lineno, Function *func = NULL);
57     void    new_include_file(SourceFile *source, Function *func);
58     void    end_include_file(Function *func);
59     void    push_src_files(Function *func);
60 
61   private:
62     Vector<SrcFileInfo*> *stack;
63 };
64 
65 // Stabs object
66 class Stabs {
67   public:
68 
69     enum Stab_status {
70 	DBGD_ERR_NONE,
71 	DBGD_ERR_CANT_OPEN_FILE,
72 	DBGD_ERR_BAD_ELF_LIB,
73 	DBGD_ERR_BAD_ELF_FORMAT,
74 	DBGD_ERR_NO_STABS,
75 	DBGD_ERR_BAD_STABS,
76 	DBGD_ERR_NO_DWARF,
77 	DBGD_ERR_CHK_SUM
78     };
79 
80     static Stabs *NewStabs(char *_path, char *lo_name);
81     Stabs(char *_path, char *_lo_name);
82     ~Stabs();
83 
is_relocatable()84     bool	is_relocatable(){ return isRelocatable; }
get_textsz()85     long long	get_textsz()	{ return textsz; }
get_platform()86     Platform_t	get_platform()	{ return platform; }
get_class()87     WSize_t	get_class()	{ return wsize;}
get_status()88     Stab_status	get_status()    { return status;}
89 
90     Stab_status	read_stabs(ino64_t srcInode, Module *module, Vector<ComC*> *comComs, bool readDwarf = false);
91     Stab_status	read_archive(LoadObject *lo);
92     bool	read_symbols(Vector<Function*> *functions);
93     uint64_t	mapOffsetToAddress(uint64_t img_offset);
94     char	*sym_name(uint64_t target, uint64_t instr, int flag);
95   Elf *openElf (bool dbg_info = false);
96     void        read_hwcprof_info(Module *module);
97     void        dump();
98     void        read_dwarf_from_dot_o(Module *mod);
99 
is_fortran(Sp_lang_code lc)100     static bool is_fortran(Sp_lang_code lc) { return (lc == Sp_lang_fortran) || (lc == Sp_lang_fortran90); }
101     static Function *find_func(char *fname, Vector<Function*> *functions, bool fortran, bool inner_names=false);
102     Module	*append_Module(LoadObject *lo, char *name, int lastMod = 0);
103     Function	*append_Function(Module *module, char *fname);
104     Function	*append_Function(Module *module, char *linkerName, uint64_t pc);
105     Function	*map_PC_to_func(uint64_t pc, uint64_t &low_pc, Vector<Function*> *functions);
106     char		*path;			// path to the object file
107     char                *lo_name;       // User name of load object
108 
109   private:
110   Elf *elfDbg; // ELF with debug info
111   Elf *elfDis; // ELF for disasm
112     Stab_status		status;			// current stabs status
113 
114     long long		textsz;			// text segment size
115     Platform_t		platform;		// Sparc, Sparcv9, Intel
116     WSize_t		wsize;			// word size: 32 or 64
117     bool		isRelocatable;
118     Symbol              *last_PC_to_sym;
119 
120     Vector<cpf_stabs_t> analyzerInfoMap;        // stabs->section mapping
121 
122     bool		check_Comm(Vector<ComC*> *comComs);
123     void		check_Info(Vector<ComC*> *comComs);
124     void		check_Loop(Vector<ComC*> *comComs);
125     void                check_AnalyzerInfo();
126     void                append_local_funcs(Module *module, int first_ind);
127   Stab_status srcline_Stabs (Module *module, unsigned int StabSec, unsigned int StabStrSec, bool comdat);
128   Stab_status archive_Stabs (LoadObject *lo, unsigned int StabSec, unsigned int StabStrSec, bool comdat);
129 
130     // Interface with Elf Symbol Table
131     void                check_Symtab();
132     void                readSymSec(unsigned int sec, Elf *elf);
133     void                check_Relocs();
134     void                get_save_addr(bool need_swap_endian);
135     Symbol              *map_PC_to_sym(uint64_t pc);
136     Symbol              *pltSym;
137     Vector<Symbol*>	*SymLst;		// list of func symbols
138     Vector<Symbol*>	*SymLstByName;		// list of func symbols sorted by Name
139     Vector<Reloc*>	*RelLst;		// list of text relocations
140     Vector<Reloc*>	*RelPLTLst;		// list of PLT relocations
141     Vector<Symbol*>	*LocalLst;		// list of local func symbols
142     Vector<char*>	*LocalFile;		// list of local files
143     Vector<int>		*LocalFileIdx;		// start index in LocalLst
144 
145     Elf         *openElf(char *fname, Stab_status &st);
146     Map<const char*, Symbol*> *get_elf_symbols();
147     Dwarf       *dwarf;
148 
149     bool        st_check_symtab, st_check_relocs;
150     Function	*createFunction(LoadObject *lo, Module *module, Symbol *sym);
151     void        fixSymtabAlias();
152 
153     // Interface with dwarf
154     Dwarf       *openDwarf();
155 
156     Vector<Module*> *stabsModules;
157     static char *get_type_name(int t);
158 };
159 
160 #endif  /* _STABS_H */
161