1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 1999-2021. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 #ifndef _BEAM_LOAD_H
22 #  define _BEAM_LOAD_H
23 
24 #include "beam_opcodes.h"
25 #include "erl_process.h"
26 
27 Eterm beam_make_current_old(Process *c_p, ErtsProcLocks c_p_locks,
28 			    Eterm module);
29 
30 typedef struct gen_op_entry {
31    char* name;
32    int arity;
33    int specific;
34    int num_specific;
35    int transform;
36 } GenOpEntry;
37 
38 extern const GenOpEntry gen_opc[];
39 
40 struct ErtsLiteralArea_;
41 
42 /*
43  * The following variables keep a sorted list of address ranges for
44  * each module.  It allows us to quickly find a function given an
45  * instruction pointer.
46  */
47 
48 /* Total code size in bytes */
49 extern Uint erts_total_code_size;
50 
51 typedef struct BeamCodeLineTab_ BeamCodeLineTab;
52 
53 /*
54  * Header of code chunks which contains additional information
55  * about the loaded module.
56  */
57 typedef struct beam_code_header {
58     /*
59      * Number of functions.
60      */
61     UWord num_functions;
62 
63     /*
64      * The attributes retrieved by Mod:module_info(attributes).
65      */
66     byte* attr_ptr;
67     UWord attr_size;
68     UWord attr_size_on_heap;
69 
70     /*
71      * The compilation information retrieved by Mod:module_info(compile).
72      */
73     byte* compile_ptr;
74     UWord compile_size;
75     UWord compile_size_on_heap;
76 
77     /*
78      * Literal area (constant pool).
79      */
80     struct ErtsLiteralArea_ *literal_area;
81 
82     /*
83      * Pointer to the on_load function (or NULL if none).
84      */
85     BeamInstr* on_load_function_ptr;
86 
87     /*
88      * Pointer to the line table (or NULL if none).
89      */
90     BeamCodeLineTab* line_table;
91 
92     /*
93      * Pointer to the module MD5 sum (16 bytes)
94      */
95     byte* md5_ptr;
96 
97     /*
98      * Start of function pointer table.  This table contains pointers to
99      * all functions in the module plus an additional pointer just beyond
100      * the end of the last function.
101      *
102      * The actual loaded code (for the first function) start just beyond
103      * this table.
104      */
105     ErtsCodeInfo* functions[1];
106 
107 }BeamCodeHeader;
108 
109 #  define BEAM_NATIVE_MIN_FUNC_SZ 4
110 
111 void erts_release_literal_area(struct ErtsLiteralArea_* literal_area);
112 int erts_is_module_native(BeamCodeHeader* code);
113 int erts_is_function_native(ErtsCodeInfo*);
114 void erts_beam_bif_load_init(Uint);
115 Uint erts_get_outstanding_system_requests_limit(void);
116 Uint erts_set_outstanding_system_requests_limit(Uint new_val);
117 struct erl_fun_entry;
118 void erts_purge_state_add_fun(struct erl_fun_entry *fe);
119 Export *erts_suspend_process_on_pending_purge_lambda(Process *c_p,
120                                                      struct erl_fun_entry*);
121 
122 /*
123  * Layout of the line table.
124  */
125 struct BeamCodeLineTab_ {
126     Eterm* fname_ptr;
127     int loc_size;
128     union {
129         Uint16* p2;
130         Uint32* p4;
131     }loc_tab;
132     const BeamInstr** func_tab[1];
133 };
134 
135 #define LINE_INVALID_LOCATION (0)
136 
137 /*
138  * Macros for manipulating locations.
139  */
140 
141 #define IS_VALID_LOCATION(File, Line) \
142     ((unsigned) (File) < 255 && (unsigned) (Line) < ((1 << 24) - 1))
143 #define MAKE_LOCATION(File, Line) (((File) << 24) | (Line))
144 #define LOC_FILE(Loc) ((Loc) >> 24)
145 #define LOC_LINE(Loc) ((Loc) & ((1 << 24)-1))
146 
147 
148 /*
149  * MFA event debug "tracing" usage:
150  *
151  * #define ENABLE_DBG_TRACE_MFA
152  * call dbg_set_traced_mfa("mymod","myfunc",arity)
153  * for the function(s) to trace, in some init function.
154  *
155  * Run and get stderr printouts when interesting things happen to your MFA.
156  */
157 #ifdef  ENABLE_DBG_TRACE_MFA
158 
159 void dbg_set_traced_mfa(const char* m, const char* f, Uint a);
160 int dbg_is_traced_mfa(Eterm m, Eterm f, Uint a);
161 void dbg_vtrace_mfa(unsigned ix, const char* format, ...);
162 #define DBG_TRACE_MFA(M,F,A,FMT, ...) do {\
163     unsigned ix;\
164     if ((ix=dbg_is_traced_mfa(M,F,A))) \
165         dbg_vtrace_mfa(ix, FMT"\n", ##__VA_ARGS__);\
166   }while(0)
167 
168 #define DBG_TRACE_MFA_P(MFA, FMT, ...) \
169         DBG_TRACE_MFA((MFA)->module, (MFA)->function, (MFA)->arity, FMT, ##__VA_ARGS__)
170 
171 #else
172 #  define dbg_set_traced_mfa(M,F,A)
173 #  define DBG_TRACE_MFA(M,F,A,FMT, ...)
174 #  define DBG_TRACE_MFA_P(MFA,FMT, ...)
175 #endif /* ENABLE_DBG_TRACE_MFA */
176 
177 #endif /* _BEAM_LOAD_H */
178