Debian Codegraph @

output/outlib.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright 1996-2020 The NASM Authors - All Rights Reserved */
3
4 /*
5 * outlib.c
6 *
7 * Common routines for the output backends.
8 */
9
10 #include "outlib.h"
11 #include "raa.h"
12
13 uint64_t realsize(enum out_type type, uint64_t size)
14 {
15 switch (type) {
16 case OUT_REL1ADR:
17 return 1;
18 case OUT_REL2ADR:
19 return 2;
20 case OUT_REL4ADR:
21 return 4;
22 case OUT_REL8ADR:
23 return 8;
24 default:
25 return size;
26 }
27 }
28
29 /* Common section/symbol handling */
30
31 struct ol_sect *_ol_sect_list;
32 uint64_t _ol_nsects; /* True sections, not external symbols */
33 static struct ol_sect **ol_sect_tail = &_ol_sect_list;
34 static struct hash_table ol_secthash;
35 static struct RAA *ol_sect_index_tbl;
36
37 struct ol_sym *_ol_sym_list;
38 uint64_t _ol_nsyms;
39 static struct ol_sym **ol_sym_tail = &_ol_sym_list;
40 static struct hash_table ol_symhash;
41
42 void ol_init(void)
43 {
44 }
45
46 static void ol_free_symbols(void)
47 {
48 struct ol_sym *s, *stmp;
49
50 hash_free(&ol_symhash);
51
52 list_for_each_safe(s, stmp, _ol_sym_list) {
53 nasm_free((char *)s->name);
54 nasm_free(s);
55 }
56
57 _ol_nsyms = 0;
58 _ol_sym_list = NULL;
59 ol_sym_tail = &_ol_sym_list;
60 }
61
62 static void ol_free_sections(void)
63 {
64 struct ol_sect *s, *stmp;
65
66 hash_free(&ol_secthash);
67 raa_free(ol_sect_index_tbl);
68 ol_sect_index_tbl = NULL;
69
70 list_for_each_safe(s, stmp, _ol_sect_list) {
71 saa_free(s->data);
72 saa_free(s->reloc);
73 nasm_free((char *)s->name);
74 nasm_free(s);
75 }
76
77 _ol_nsects = 0;
78 _ol_sect_list = NULL;
79 ol_sect_tail = &_ol_sect_list;
80 }
81
82 void ol_cleanup(void)
83 {
84 ol_free_symbols();
85 ol_free_sections();
86 }
87
88 /*
89 * Allocate a section index and add a section, subsection, or external
90 * symbol to the section-by-index table. If the index provided is zero,
91 * allocate a new index via seg_alloc().
92 */
93 static uint32_t ol_seg_alloc(void *s, uint32_t ix)
94 {
95 if (!ix)
96 ix = seg_alloc();
97 ol_sect_index_tbl = raa_write_ptr(ol_sect_index_tbl, ix >> 1, s);
98 return ix;
99 }
100
101 /*
102 * Find a section or create a new section structure if it does not exist
103 * and allocate it an index value via seg_alloc().
104 */
105 struct ol_sect *_ol_get_sect(const char *name, size_t ssize, size_t rsize)
106 {
107 struct ol_sect *s, **sp;
108 struct hash_insert hi;
109
110 sp = (struct ol_sect **)hash_find(&ol_secthash, name, &hi);
111 if (sp)
112 return *sp;
113
114 s = nasm_zalloc(ssize);
115 s->syml.tail = &s->syml.head;
116 s->name = nasm_strdup(name);
117 s->data = saa_init(1);
118 s->reloc = saa_init(rsize);
119 *ol_sect_tail = s;
120 ol_sect_tail = &s->next;
121 _ol_nsects++;
122 s->index = s->subindex = ol_seg_alloc(s, 0);
123
124 hash_add(&hi, s->name, s);
125 return s;
126 }
127
128 /* Find a section by name without creating one */
129 struct ol_sect *_ol_sect_by_name(const char *name)
130 {
131 struct ol_sect **sp;
132
133 sp = (struct ol_sect **)hash_find(&ol_secthash, name, NULL);
134 return sp ? *sp : NULL;
135 }
136
137 /* Find a section or external symbol by index; NULL if not valid */
138 struct ol_sect *_ol_sect_by_index(int32_t index)
139 {
140 uint32_t ix = index;
141
142 if (unlikely(ix >= SEG_ABS))
143 return NULL;
144
145 return raa_read_ptr(ol_sect_index_tbl, ix >> 1);
146 }
147
148 /*
149 * Start a new subsection for the given section. At the moment, once a
150 * subsection has been created, it is not possible to revert to an
151 * earlier subsection. ol_sect_by_index() will return the main section
152 * structure. Returns the new section index. This is used to prevent
153 * the front end from optimizing across subsection boundaries.
154 */
155 int32_t _ol_new_subsection(struct ol_sect *sect)
156 {
157 if (unlikely(!sect))
158 return NO_SEG;
159
160 return sect->subindex = ol_seg_alloc(sect, 0);
161 }
162
163 /*
164 * Insert a symbol into a list; need to use upcasting using container_of()
165 * to walk the list later.
166 */
167 static void ol_add_sym_to(struct ol_symlist *syml, struct ol_symhead *head,
168 uint64_t offset)
169 {
170 syml->tree.key = offset;
171 head->tree = rb_insert(head->tree, &syml->tree);
172 *head->tail = syml;
173 head->tail = &syml->next;
174 head->n++;
175 }
176
177 /*
178 * Create a location structure from seg:offs
179 */
180 void ol_mkloc(struct ol_loc *loc, int64_t offs, int32_t seg)
181 {
182 nasm_zero(*loc);
183 loc->offs = offs;
184
185 if (unlikely((uint32_t)seg >= SEG_ABS)) {
186 if (likely(seg == NO_SEG)) {
187 loc->seg.t = OS_NOSEG;
188 } else {
189 loc->seg.t = OS_ABS;
190 loc->seg.index = seg - SEG_ABS;
191 }
192 } else {
193 loc->seg.index = seg & ~1;
194 loc->seg.t = OS_SECT | (seg & 1);
195 loc->seg.s.sect = _ol_sect_by_index(loc->seg.index);
196 }
197 }
198
199 /*
200 * Create a new symbol. If this symbol is OS_OFFS, add it to the relevant
201 * section, too. If the symbol already exists, return NULL; this is
202 * different from ol_get_section() as a single section may be invoked
203 * many times. On the contrary, the front end will prevent a single symbol
204 * from being defined more than once.
205 *
206 * If flags has OF_GLOBAL set, add it to the global symbol hash for
207 * the containing section if applicable.
208 *
209 * If flags has OF_IMPSEC set, allocate a segment index for it via
210 * seg_alloc() unless v->index is already set, and add it to the
211 * section by index list.
212 */
213 struct ol_sym *_ol_new_sym(const char *name, const struct ol_loc *v,
214 uint32_t flags, size_t size)
215 {
216 struct hash_insert hi;
217 struct ol_sym *sym;
218
219 if (hash_find(&ol_symhash, name, &hi))
220 return NULL; /* Symbol already exists */
221
222 flags |= OF_SYMBOL;
223
224 sym = nasm_zalloc(size);
225 sym->name = nasm_strdup(name);
226 sym->v = *v;
227
228 if (sym->v.seg.t & OS_SECT) {
229 struct ol_sect *sect = sym->v.seg.s.sect;
230
231 if (!sect || (sect->flags & OF_SYMBOL))
232 /* Must be an external or common reference */
233 flags |= OF_IMPSEC;
234
235 if (flags & OF_IMPSEC) {
236 /* Metasection */
237 if (!sym->v.seg.s.sym) {
238 sym->v.seg.s.sym = sym;
239 sym->v.seg.index = ol_seg_alloc(sym, sym->v.seg.index);
240 }
241 } else if (sym->v.seg.t == OS_OFFS) {
242 struct ol_sect * const sect = sym->v.seg.s.sect;
243 const uint64_t offs = sym->v.offs;
244
245 ol_add_sym_to(&sym->syml, &sect->syml, offs);
246 if (flags & OF_GLOBAL)
247 ol_add_sym_to(&sym->symg, &sect->symg, offs);
248 }
249 }
250 sym->flags = flags;
251
252 *ol_sym_tail = sym;
253 ol_sym_tail = &sym->next;
254 _ol_nsyms++;
255
256 hash_add(&hi, sym->name, sym);
257 return sym;
258 }
259
260 /* Find a symbol in the global namespace */
261 struct ol_sym *_ol_sym_by_name(const char *name)
262 {
263 struct ol_sym **symp;
264
265 symp = (struct ol_sym **)hash_find(&ol_symhash, name, NULL);
266 return symp ? *symp : NULL;
267 }
268
269 /*
270 * Find a symbol by address in a specific section. If no symbol is defined
271 * at that exact address, return the immediately previously defined one.
272 * If global is set, then only return global symbols.
273 */
274 struct ol_sym *_ol_sym_by_address(struct ol_sect *sect, int64_t addr,
275 bool global)
276 {
277 struct ol_symhead *head;
278 size_t t_offs;
279 struct rbtree *t;
280
281 if (global) {
282 head = &sect->symg;
283 t_offs = offsetof(struct ol_sym, symg.tree);
284 } else {
285 head = &sect->syml;
286 t_offs = offsetof(struct ol_sym, syml.tree);
287 }
288
289 t = rb_search(head->tree, addr);
290 if (!t)
291 return NULL;
292
293 return (struct ol_sym *)((char *)t - t_offs);
294 }