1 
2 /*
3  * %CopyrightBegin%
4  *
5  * Copyright Ericsson AB 2020-2020. All Rights Reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * %CopyrightEnd%
20  */
21 
22 #ifndef _EMU_LOAD_H
23 #define _EMU_LOAD_H
24 
25 /*
26  * Type for a reference to a label that must be patched.
27  */
28 
29 typedef struct {
30     Uint pos;                   /* Position of label reference to patch. */
31     Uint offset;                /* Offset from patch location.  */
32     int packed;                 /* 0 (not packed), 1 (lsw), 2 (msw) */
33 } LabelPatch;
34 
35 /*
36  * Type for a label.
37  */
38 typedef struct {
39     Uint value;                /* Value of label (0 if not known yet). */
40     Uint looprec_targeted;     /* Non-zero if this label is the target of a
41                                 * loop_rec instruction.
42                                 */
43     LabelPatch* patches;        /* Array of label patches. */
44     Uint num_patches;           /* Number of patches in array. */
45     Uint num_allocated;         /* Number of allocated patches. */
46 } Label;
47 
48 /*
49  * This structure keeps load-time information about a literal.
50  */
51 
52 typedef struct {
53     Eterm term;                     /* The tagged term (in the heap). */
54     ErlHeapFragment* heap_frags;
55 } Literal;
56 
57 /*
58  * This structure keeps information about an operand that needs to be
59  * patched to contain the correct address of a literal when the code is
60  * frozen.
61  */
62 
63 typedef struct literal_patch {
64     Uint pos;                        /* Position in code */
65     struct literal_patch *next;
66 } LiteralPatch;
67 
68 /*
69  * This structure keeps information about an operand that needs to be
70  * patched to contain the correct address for an address into the string table.
71  */
72 
73 typedef struct string_patch {
74     Uint pos;                        /* Position in code */
75     struct string_patch *next;
76 } StringPatch;
77 
78 typedef struct lambda_patch {
79     Uint pos;                        /* Position in code */
80     struct lambda_patch *next;
81 } LambdaPatch;
82 
83 /*
84  * This structure associates a code offset with a source code location.
85  */
86 
87 typedef struct {
88     int pos;                    /* Position in code */
89     int loc;                    /* Location in source code */
90 } LineInstr;
91 
92 /*
93  * This structure contains all information about the module being loaded.
94  */
95 struct LoaderState_ {
96     /*
97      * The current logical file within the binary.
98      */
99     ErlDrvBinary* bin;         /* Binary holding BEAM file (or NULL) */
100 
101     /*
102      * The following are used mainly for diagnostics.
103      */
104 
105     Eterm group_leader;        /* Group leader (for diagnostics). */
106     Eterm module;              /* Tagged atom for module name. */
107     Eterm function;            /* Tagged atom for current function
108                                 * (or 0 if none).
109                                 */
110     unsigned arity;            /* Arity for current function. */
111 
112     /*
113      * Used for code loading (mainly).
114      */
115 
116     int specific_op;           /* Specific opcode (-1 if not found). */
117     BeamCodeHeader* code_hdr;   /* Code header */
118 
119     BeamInstr* codev;          /* Loaded code buffer */
120     int        codev_size;     /* Size of code buffer in words. */
121     int ci;                    /* Current index into loaded code buffer. */
122     Label* labels;
123     unsigned loaded_size;      /* Final size of code when loaded. */
124     int may_load_nif;          /* true if NIFs may be loaded for this module */
125     int on_load;               /* Index in the code for the on_load function
126                                 * (or 0 if there is no on_load function)
127                                 */
128 
129     /*
130      * Generic instructions.
131      */
132     BeamOp* genop;		/* The last generic instruction seen. */
133 
134     BifEntry **bif_imports;
135 
136     BeamInstr catches;		/* Linked list of catch_yf instructions. */
137     BeamInstr *import_patches; /* Linked lists of import entries. */
138 
139     LambdaPatch* lambda_patches; /* Linked list of position into fun table to patch. */
140     LiteralPatch* literal_patches; /* Operands that need to be patched. */
141     StringPatch* string_patches; /* Linked list of position into string table to patch. */
142 
143     /*
144      * Line table.
145      */
146     LineInstr* line_instr;	/* Line instructions */
147     unsigned int current_li;	/* Current line instruction */
148     unsigned int* func_line;	/* Mapping from function to first line instr */
149 
150     int otp_20_or_higher;
151 
152     Uint last_func_start;
153     int function_number;
154     int last_label;
155 
156     BeamOpAllocator op_allocator;
157     BeamFile beam;
158 };
159 
160 #endif
161